aboutsummaryrefslogtreecommitdiff
path: root/ext/js/background/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-08-07 17:17:53 -0400
committerGitHub <noreply@github.com>2021-08-07 17:17:53 -0400
commit69e448a95547479bb595530dcbb89bfe017a8aed (patch)
treebb77e17a10de855497b7b93f7bb07df4e6203f71 /ext/js/background/backend.js
parent2d5e6f839412985ae790da34b3f75476bcf8028c (diff)
Add support for manifest V3 script injection (#1878)
Diffstat (limited to 'ext/js/background/backend.js')
-rw-r--r--ext/js/background/backend.js67
1 files changed, 57 insertions, 10 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index b4b9bc27..e293a3a7 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -2164,6 +2164,61 @@ class Backend {
});
}
+ _injectScript(file, tabId, frameId) {
+ if (isObject(chrome.tabs) && typeof chrome.tabs.executeScript === 'function') {
+ return this._injectScriptMV2(file, tabId, frameId);
+ } else if (isObject(chrome.scripting) && typeof chrome.scripting.executeScript === 'function') {
+ return this._injectScriptMV3(file, tabId, frameId);
+ } else {
+ return Promise.reject(new Error('executeScript function not available'));
+ }
+ }
+
+ _injectScriptMV2(file, tabId, frameId) {
+ return new Promise((resolve, reject) => {
+ const details = {
+ allFrames: false,
+ frameId,
+ file,
+ matchAboutBlank: true,
+ runAt: 'document_start'
+ };
+ const callback = (results) => {
+ const e = chrome.runtime.lastError;
+ if (e) {
+ reject(new Error(e.message));
+ } else {
+ const result = results[0];
+ resolve({frameId, result});
+ }
+ };
+ chrome.tabs.executeScript(tabId, details, callback);
+ });
+ }
+
+ _injectScriptMV3(file, tabId, frameId) {
+ return new Promise((resolve, reject) => {
+ const details = {
+ files: [file],
+ target: {
+ allFrames: false,
+ frameIds: [frameId],
+ tabId
+ }
+ };
+ const callback = (results) => {
+ const e = chrome.runtime.lastError;
+ if (e) {
+ reject(new Error(e.message));
+ } else {
+ const {frameId: frameId2, result} = results[0];
+ resolve({frameId: frameId2, result});
+ }
+ };
+ chrome.scripting.executeScript(details, callback);
+ });
+ }
+
_getTabById(tabId) {
return new Promise((resolve, reject) => {
chrome.tabs.get(
@@ -2215,7 +2270,7 @@ class Backend {
}
}
- _updateTabAccessibility(url, tab, frameId) {
+ async _updateTabAccessibility(url, tab, frameId) {
let file = null;
switch (new URL(url).hostname) {
@@ -2231,14 +2286,6 @@ class Backend {
if (file === null) { return; }
- const details = {
- allFrames: false,
- frameId,
- file,
- matchAboutBlank: true,
- runAt: 'document_start'
- };
- const callback = () => this._checkLastError(chrome.runtime.lastError);
- chrome.tabs.executeScript(tab.id, details, callback);
+ return await this._injectScript(file, tab.id, frameId);
}
}