-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathoptions.js
More file actions
100 lines (86 loc) · 2.8 KB
/
options.js
File metadata and controls
100 lines (86 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const USER_SCRIPT_ID = 'default';
const SAVE_BUTTON_ID = 'save-button';
const FORM_ID = 'settings-form';
const FORM = document.getElementById(FORM_ID);
const TYPE_INPUT_NAME = 'type';
const SCRIPT_TEXTAREA_NAME = 'custom-script';
/**
* Checks if the user has developer mode enabled, which is required to use the
* User Scripts API.
*
* @returns If the chrome.userScripts API is available.
*/
function isUserScriptsAvailable() {
try {
// Property access which throws if developer mode is not enabled.
chrome.userScripts;
return true;
} catch {
// Not available, so hide UI and show error.
document.getElementById('warning').style.display = 'block';
FORM.style.display = 'none';
return false;
}
}
async function updateUi() {
if (!isUserScriptsAvailable()) return;
// Access settings from storage with default values.
const { type, script } = await chrome.storage.local.get({
type: 'file',
script: "alert('hi');"
});
// Update UI with current values.
FORM.elements[TYPE_INPUT_NAME].value = type;
FORM.elements[SCRIPT_TEXTAREA_NAME].value = script;
}
async function onSave() {
if (!isUserScriptsAvailable()) return;
// Get values from form.
const type = FORM.elements[TYPE_INPUT_NAME].value;
const script = FORM.elements[SCRIPT_TEXTAREA_NAME].value;
// Save to storage.
chrome.storage.local.set({
type,
script
});
const existingScripts = await chrome.userScripts.getScripts({
ids: [USER_SCRIPT_ID]
});
if (existingScripts.length > 0) {
// Update existing script.
await chrome.userScripts.update([
{
id: USER_SCRIPT_ID,
matches: ['https://example.com/*'],
js: type === 'file' ? [{ file: 'user-script.js' }] : [{ code: script }]
}
]);
} else {
// Register new script.
await chrome.userScripts.register([
{
id: USER_SCRIPT_ID,
matches: ['https://example.com/*'],
js: type === 'file' ? [{ file: 'user-script.js' }] : [{ code: script }]
}
]);
}
}
// Update UI immediately, and on any storage changes.
updateUi();
chrome.storage.local.onChanged.addListener(updateUi);
// Register listener for save button click.
document.getElementById(SAVE_BUTTON_ID).addEventListener('click', onSave);