Skip to content

Commit

Permalink
change logic so that instead of using catches, a background script is…
Browse files Browse the repository at this point in the history
… used to check for already injected scripts. Now works with firefox
  • Loading branch information
thesofakillers committed Nov 18, 2019
1 parent d6d8276 commit 274d5f9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
21 changes: 21 additions & 0 deletions GPTrueOrFalse/src/background/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// initialize object containing ids of tabs who have been injected
let injected = {};
browser.runtime.onMessage.addListener(gotMessage);
function gotMessage(message) {
console.log(injected);
if (message.type === "get-injected") {
return Promise.resolve(injected);
} else if (message.type === "injection-update") {
if (message.content.task === "append") {
injected[message.content.value] = true;
} else if (message.content.task === "delete") {
delete injected[message.content.value];
}
}
}

// remove tabs from injected obj when they are closed
browser.tabs.onRemoved.addListener(handleRemoved);
function handleRemoved(tabId) {
if (injected[tabId]) delete injected[tabId];
}
4 changes: 1 addition & 3 deletions GPTrueOrFalse/src/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ function gotMessage(message) {
return checkSelection();
} else if (message.type === "evaluate") {
return evaluateSelection();
} else if (message.type === "loaded-check") {
return Promise.resolve({ reply: true });
}
}

Expand Down Expand Up @@ -89,7 +87,7 @@ function getSelectedText() {
return text;
}

// generates HSL string given on a percentage between a start and end color
// generates HSL string given a percentage between a start and end color
function generateHSLString(percent, start, end) {
let fraction = percent / 100;
let offset = (end - start) * fraction;
Expand Down
6 changes: 6 additions & 0 deletions GPTrueOrFalse/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@
},
"default_popup": "popup/index.html",
"default_title": "GPTrue or False"
},
"background": {
"scripts": [
"./libraries/browser-polyfill.min.js",
"./background/background.js"
]
}
}
65 changes: 38 additions & 27 deletions GPTrueOrFalse/src/popup/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
window.onload = () => {
// get the current tab
//check if the ask background script for list of tabs with loaded
return (
browser.tabs
.query({ active: true, currentWindow: true })
.then(query_result => {
// ask it if the content script is already loaded
return browser.tabs.sendMessage(query_result[0].id, {
type: "loaded-check"
});
})
.then(res => {
// if it is, do nothing for this promise
if (res) return Promise.resolve();
})
.catch(() => {
// an error being thrown signals the content script not being loaded, load it.
browser.runtime
.sendMessage({ type: "get-injected" })
.then(injected_obj => {
return browser.tabs
.executeScript({
file: "./libraries/jquery-3.3.1.slim.min.js"
})
.then(() => {
return browser.tabs.executeScript({
file: "./libraries/browser-polyfill.min.js"
});
})
.then(() => {
return browser.tabs.executeScript({
file: "./content/content.js"
});
.query({ active: true, currentWindow: true })
.then(query_result => {
let current_tab_id = query_result[0].id;
// if the current tab has already been injected
if (injected_obj[current_tab_id]) {
return Promise.resolve();
} else {
// update the injected list in the background script
return browser.runtime
.sendMessage({
type: "injection-update",
content: {
task: "append",
value: current_tab_id
}
})
.then(() => {
// inject the necessary scripts otherwise.
return browser.tabs.executeScript({
file: "/libraries/jquery-3.3.1.slim.min.js"
});
})
.then(() => {
return browser.tabs.executeScript({
file: "/libraries/browser-polyfill.min.js"
});
})
.then(() => {
return browser.tabs.executeScript({
file: "/content/content.js"
});
});
}
});
})
// check that enough words are selected
Expand All @@ -48,6 +58,7 @@ window.onload = () => {
return deactivateButton("evaluateSelectedText", selected_words);
}
})
.catch(err => console.log(err))
);
};

Expand Down

0 comments on commit 274d5f9

Please sign in to comment.