Skip to content

Commit

Permalink
Migrate to manifest v3 for Opera
Browse files Browse the repository at this point in the history
  • Loading branch information
SinclaM committed Jul 19, 2022
1 parent 89c3cb1 commit e464b82
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
28 changes: 20 additions & 8 deletions public/opera_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"name": "Desmos Unlocked",
"version": "1.0.4",
"description": "Browser extension for better user control of the Desmos graphing calculator configuration",
"permissions": ["https://*.desmos.com/*", "storage", "webRequest", "webRequestBlocking"],
"manifest_version": 2,
"permissions": ["storage", "declarativeNetRequest", "declarativeNetRequestWithHostAccess", "webRequest"],
"manifest_version": 3,
"background": {
"scripts": ["browser-polyfill.js", "background.js"],
"persistent": true
"service_worker": "background.js"
},
"content_scripts": [
{
Expand All @@ -20,10 +19,14 @@
"run_at": "document_start"
}
],

"web_accessible_resources": ["script.js", "preloadScript.js"],

"browser_action": {
"host_permissions": ["*://www.desmos.com/calculator/*"],
"web_accessible_resources": [
{
"resources": ["browser-polyfill.js", "script.js", "preloadScript.js", "empty.js"],
"matches": ["<all_urls>"]
}
],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/logo/16.png",
Expand All @@ -32,6 +35,15 @@
"128": "images/logo/128.png"
}
},
"declarative_net_request": {
"rule_resources": [
{
"id": "ruleset_1",
"enabled": true,
"path": "net_request_rules.json"
}
]
},
"icons": {
"16": "images/logo/16.png",
"32": "images/logo/32.png",
Expand Down
54 changes: 22 additions & 32 deletions src/background/opera/background.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
/// <reference types="web-ext-types"/>
/// <reference types="chrome"/>

browser.runtime.onInstalled.addListener(function () {
browser.storage.local.set({
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.local.set({
// keepmeKEEPME should always remain in autoCommands, since MathQuill requires a nonempty
// space-delimited list of commands.
autoCommands:
"keepmeKEEPME alpha beta sqrt theta Theta phi Phi pi Pi tau nthroot cbrt sum prod int ans percent infinity infty gamma Gamma delta Delta epsilon epsiv zeta eta kappa lambda Lambda mu xi Xi rho sigma Sigma chi Psi omega Omega digamma iota nu upsilon Upsilon Psi square mid parallel nparallel perp to times div approx",
});
});

browser.webRequest.onBeforeRequest.addListener(
// Listen for redirects from the a request to calculator_desktop. When this redirect happens,
// we want to send a message to the preload content script to begin module overrides and
// initialize the calculator.
chrome.webRequest.onBeforeRedirect.addListener(
async function ({ url }) {
// This url is used by preload_content to check for DesModder activity.
// We ignore it here to avoid an infinite recursion.
if (url === "https://www.desmos.com/assets/build/calculator_desktop-this_does_not_exist.js") {
return;
}

const tabs = await browser.tabs.query({ active: true, currentWindow: true });

if (url.endsWith(".js")) {
const isDesmodderActive: boolean | void = await browser.tabs.sendMessage(tabs[0].id, {
message: "check-desmodder",
});

// If DesModder is not active, then we should immediately inject the preload script
// and begin overrides. There won't be another opportunity.
// If DesModder is active, then we know that DesModder's own webRequest rules will
// block this script anyway, and we don't want to define ALMOND_OVERRIDES before
// DesModder does, so we can wait until DesModder makes another request.
if (!isDesmodderActive) {
await browser.tabs.sendMessage(tabs[0].id, { message: "inject-preload" });
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
while (true) {
// The request to calculator_desktop (and the following redirect) may happen
// before preload_content is loaded at document_start. To avoid a race condition,
// we keep trying to send the message until the "no receiving end" exception
// is gone.
try {
// TODO: Figure out why tsserver thinks that sendMessage returns void and not
// a promise. tsserver says await has no effect on the expression below but it's
// actual critical to resolve the promise and catch the error.
await chrome.tabs.sendMessage(tabs[0].id, { message: "redirect-detected", url: url });
break;
} catch {
await new Promise((r) => setTimeout(r, 10));
}
}

// This url being requested means DesModder is active and trying to load the calculator.
// Now is the time to inject the preload.
if (url.endsWith(".js?")) {
await browser.tabs.sendMessage(tabs[0].id, { message: "inject-preload" });
}
},
{
urls: [
"https://www.desmos.com/assets/build/calculator_desktop-*.js",
"https://www.desmos.com/assets/build/calculator_desktop-*.js?",
],
},
["blocking"]
}
);

0 comments on commit e464b82

Please sign in to comment.