Activate extension on all pages

fixes incompatibilities with firefox and makes code easier to maintain
This commit is contained in:
Sebastian Sauer 2023-07-18 22:20:39 +02:00
parent e56335dd45
commit 2b263859e6
4 changed files with 10 additions and 94 deletions

@ -1,6 +1,6 @@
{
"name": "gitea conventional comments buttons",
"version": "0.0.1",
"version": "0.0.2",
"manifest_version": 3,
"description": "An extension to quickly add conventional comments",
"homepage_url": "https://conventionalcomments.org/",
@ -11,18 +11,18 @@
},
"default_locale": "en",
"action": {},
"options_ui": {
"page": "src/options.html"
},
"permissions": [
"activeTab",
"storage",
"scripting"
],
"host_permissions": [
"*://*/"
"*://*/*"
],
"background": {
"service_worker": "src/background.js"
}
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["src/inject/inject.js"],
"css": ["src/inject/inject.css"],
"runAt": "document_idle"
}
]
}

@ -1,40 +0,0 @@
const scriptId = "conventional-comments-button";
const defaultHost = 'https://gitea.com';
async function registerContentScripts(hosts) {
hosts = hosts.split('\n');
for (var index in hosts) {
hosts[index] = hosts[index].trim() + "/*";
}
await chrome.scripting.registerContentScripts([{
id: scriptId,
matches: hosts,
js: ["src/inject/inject.js"],
css: ["src/inject/inject.css"],
runAt: "document_idle",
}]);
}
chrome.action.onClicked.addListener(() => {
chrome.runtime.openOptionsPage();
})
chrome.storage.sync.get({ hosts: defaultHost }, async function (result) {
console.log('Hosts is currently ', result);
var hosts = result.hosts;
console.log('Setting hosts to ' + hosts);
registerContentScripts(hosts);
});
chrome.storage.onChanged.addListener(async (changes, areaName) => {
if (changes['hosts']) {
await chrome.scripting.unregisterContentScripts({ ids: [scriptId] });
console.log("Setting new hosts to " + changes['hosts']['newValue']);
await registerContentScripts(changes['hosts']['newValue']);
}
}
)

@ -1,16 +0,0 @@
<!DOCTYPE html>
<html>
<head><title>Coventional Comments Button Options</title></head>
<body>
<label>
Enabled Hosts (one per line):
</label>
<textarea id="hosts" rows="5" cols="49"></textarea>
<div id="status"></div>
<button id="save">Save</button>
<script src="options.js"></script>
</body>
</html>

@ -1,28 +0,0 @@
// Saves options to chrome.storage
function save_options() {
var hosts = document.getElementById('hosts').value;
chrome.storage.sync.set({
hosts: hosts,
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
// Use default value color = 'red' and likesColor = true.
chrome.storage.sync.get({
hosts: 'https://gitlab.com'
}, function(items) {
document.getElementById('hosts').value = items.hosts;
});
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);