Skip to content

Commit 325f8cb

Browse files
authored
Merge pull request #1 from dotproto/pr-608
Migrate to Scripting API
2 parents da2172c + 8b43ca9 commit 325f8cb

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

beastify/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Note that:
2525

2626
* if the user reloads the tab, or switches tabs, while the popup is open, then the popup won't be able to beastify the page any more (because the content script was injected into the original tab).
2727

28-
* by default [`tabs.executeScript()`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript) injects the script only when the web page and its resources have finished loading. This means that clicks in the popup will have no effect until the page has finished loading.
28+
* by default [`scripting.executeScript()`](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/scripting/executeScript) injects the script only when the web page and its resources have finished loading. This means that clicks in the popup will have no effect until the page has finished loading.
2929

3030
* it's not possible to inject content scripts into certain pages, including privileged browser pages like "about:debugging" and the [addons.mozilla.org](https://addons.mozilla.org/) website. If the user clicks the beastify icon when such a page is loaded into the active tab, the popup displays an error message.
3131

@@ -34,7 +34,7 @@ Note that:
3434
* write a browser action with a popup
3535
* how to have different browser_action images based upon the theme
3636
* give the popup style and behavior using CSS and JS
37-
* inject a content script programmatically using `tabs.executeScript()`
37+
* inject a content script programmatically using `scripting.executeScript()`
3838
* send a message from the main extension to a content script
3939
* use web accessible resources to enable web pages to load packaged content
4040
* reload web pages

beastify/popup/choose_beast.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const hidePage = `body > :not(.beastify-image) {
1212
*/
1313
function listenForClicks() {
1414
document.addEventListener("click", (e) => {
15-
1615
/**
1716
* Given the name of a beast, get the URL to the corresponding image.
1817
*/
@@ -33,25 +32,37 @@ function listenForClicks() {
3332
* send a "beastify" message to the content script in the active tab.
3433
*/
3534
function beastify(tabs) {
36-
browser.scripting.insertCSS({code: hidePage}).then(() => {
37-
const url = beastNameToURL(e.target.textContent);
38-
browser.tabs.sendMessage(tabs[0].id, {
39-
command: "beastify",
40-
beastURL: url
35+
const tabId = tabs[0].id;
36+
browser.scripting
37+
.insertCSS({
38+
target: { tabId },
39+
css: hidePage,
40+
})
41+
.then(() => {
42+
const url = beastNameToURL(e.target.textContent);
43+
browser.tabs.sendMessage(tabId, {
44+
command: "beastify",
45+
beastURL: url,
46+
});
4147
});
42-
});
4348
}
4449

4550
/**
4651
* Remove the page-hiding CSS from the active tab,
4752
* send a "reset" message to the content script in the active tab.
4853
*/
4954
function reset(tabs) {
50-
browser.scripting.removeCSS({code: hidePage}).then(() => {
51-
browser.tabs.sendMessage(tabs[0].id, {
52-
command: "reset",
55+
const tabId = tabs[0].id;
56+
browser.scripting
57+
.removeCSS({
58+
target: { tabId },
59+
css: hidePage,
60+
})
61+
.then(() => {
62+
browser.tabs.sendMessage(tabId, {
63+
command: "reset",
64+
});
5365
});
54-
});
5566
}
5667

5768
/**
@@ -68,13 +79,15 @@ function listenForClicks() {
6879
if (e.target.tagName !== "BUTTON" || !e.target.closest("#popup-content")) {
6980
// Ignore when click is not on a button within <div id="popup-content">.
7081
return;
71-
}
82+
}
7283
if (e.target.type === "reset") {
73-
browser.tabs.query({active: true, currentWindow: true})
84+
browser.tabs
85+
.query({ active: true, currentWindow: true })
7486
.then(reset)
7587
.catch(reportError);
7688
} else {
77-
browser.tabs.query({active: true, currentWindow: true})
89+
browser.tabs
90+
.query({ active: true, currentWindow: true })
7891
.then(beastify)
7992
.catch(reportError);
8093
}
@@ -96,6 +109,13 @@ function reportExecuteScriptError(error) {
96109
* and add a click handler.
97110
* If we couldn't inject the script, handle the error.
98111
*/
99-
browser.tabs.executeScript({file: "/content_scripts/beastify.js"})
100-
.then(listenForClicks)
101-
.catch(reportExecuteScriptError);
112+
browser.tabs
113+
.query({ active: true, currentWindow: true })
114+
.then(([tab]) =>
115+
browser.scripting.executeScript({
116+
target: { tabId: tab.id },
117+
files: ["/content_scripts/beastify.js"],
118+
})
119+
)
120+
.then(listenForClicks)
121+
.catch(reportExecuteScriptError);

0 commit comments

Comments
 (0)