Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ZipFile committed Aug 26, 2024
1 parent bffc6cb commit 1a951eb
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/injections/instagram.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
div:has(> img) + div:empty {pointer-events: none !important;}
6 changes: 6 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@
"contextMenus",
"storage"
],
"optional_permissions": [
"scripting"
],
"optional_host_permissions": [
"*://www.instagram.com/*"
],
"commands": {
"_execute_page_action": {
"suggested_key": {
Expand Down
7 changes: 7 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
</fieldset>
<button type="submit">Save</button>
</form>
<hr />
<form id="siteSpecific">
<fieldset>
<legend>Site-specific settings</legend>
<label><input type="checkbox" name="instagram" id="instagramFixup" />Fix Instagram Context Menu</label><br/>
</fieldset>
</form>
<script type="module" src="options.js"></script>
</body>
</html>
16 changes: 15 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
BrowserContextMenuManager,
ContextMenuSetupperImpl,
} from "./contextMenuManager.js";
import {BrowserStorageSettings, FormManager} from "./settings.js";
import {BrowserStorageSettings, FormManager, ScriptsManager} from "./settings.js";
import { DanbooruURL, getAPI } from "./utils.js";

const [api, isChrome] = getAPI(globalThis);
Expand Down Expand Up @@ -39,3 +39,17 @@ async function restoreOptions() {
document.addEventListener("DOMContentLoaded", restoreOptions);
form.addEventListener("submit", saveOptions);
form.url.placeholder = DanbooruURL;

const scripts = {
instagram: {
matches: ["*://www.instagram.com/*"],
css: ["injections/instagram.css"],
},
};
const scriptsManager = new ScriptsManager(
document.forms.siteSpecific,
api,
scripts,
);

document.addEventListener("DOMContentLoaded", scriptsManager.onLoad);
72 changes: 72 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,75 @@ export class FormManager {
}
}
}

export class ScriptsManager {
constructor(form, browser, scripts) {
this.form = form;
this.browser = browser;
this.scripts = scripts;

this.onLoad = this.onLoad.bind(this);
this.onChange = this.onChange.bind(this);
}

async onLoad() {
for (const [id, script] of Object.entries(this.scripts)) {
const checkbox = this.form[id];

if (!checkbox) {
console.warn("Form has no checkbox with name", id);
continue;
}

checkbox.addEventListener("change", this.onChange);
checkbox.checked = await this.browser.permissions.contains({
origins: script.matches,
});
}
}

onChange(e) {
const {checked, name} = e.target;

if (checked) {
return this.request(name);
}

return this.revoke(name);
}

async request(id) {
const script = this.scripts[id];

if (!script) {
return;
}

const granted = await this.browser.permissions.request({
permissions: ["scripting"],
origins: script.matches,
});

if (!granted) {
return;
}

await this.browser.scripting.registerContentScripts([{
id: id,
matches: script.matches,
css: script.css,
js: script.js,
}]);
}

async revoke(id) {
const script = this.scripts[id];

if (!script) {
return;
}

await this.browser.scripting.unregisterContentScripts({ids: [id]});
await this.browser.permissions.remove({origins: script.matches});
}
}

0 comments on commit 1a951eb

Please sign in to comment.