I am currently making a chrome extension that needs to validate whether a user has paid for the service. My background script confirms with the ExtensionPay API (thanks to Glen Chiacchieri) and then injects a script to be run. I learned that I can inject scripts from the background script to a target tab with chrome.scripting.executeScript(). But when after the code is injected, I get the following error:
Error handling response: ReferenceError: document is not defined
Here is my background.js:
importScripts("ExtPay.js", "script.js", "monitorNetflix.js");
const extpay = ExtPay("censor-bad-words-on-netflix")
extpay.startBackground();
const millisecondsInOneDay = 86400000;
/* If extpay needs to be used in callbacks...
chrome.storage.local.get("foo", function() {
var extpay = ExtPay("sample-extension");
// ...
})
*/
const authenticateUser = () => {
chrome.storage.sync.set({authenticated: true});
chrome.tabs.query({}, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
var tabId = tabs[i].id;
chrome.scripting.executeScript({
target: {tabId: tabId},
func: script
}, _=>{
let e = chrome.runtime.lastError;
if(e !== undefined){
console.log(tabId, _, e);
}
});
chrome.scripting.executeScript({
target: {tabId: tabId},
func: monitorNetflix
}, _=>{
let e = chrome.runtime.lastError;
if(e !== undefined){
console.log(tabId, _, e);
}
});
}
});
};
extpay.getUser().then(user => {
if (user.paid) {
// In the clear. They are free to use it.
console.log("User paid on: ", user.paidAt)
console.log("You are enabled into the system.");
authenticateUser();
// script();
// monitorNetflix();
} else {
if (!user.trialStartedAt) {
console.log("Start a three day trial.");
extpay.openTrialPage("3-day");
}
else if (new Date().getTime() - user.trialStartedAt.getTime() > millisecondsInOneDay * 0.01) {
console.log("Trail expired");
extpay.openPaymentPage().catch((rej) => {
console.log(rej, "Something went wrong with the network.");
});
} else {
console.log("Trial is still going.");
}
}
}).catch((rej) => {
console.log(rej, "Something went wrong with the network.");
});
extpay.onPaid.addListener(user => {
console.log("User just paid!");
authenticateUser();
// script();
// monitorNetflix();
});
extpay.onTrialStarted.addListener(user => {
console.log("User just started trial.");
authenticateUser();
// script();
// monitorNetflix();
});
And the manifest.
{
"manifest_version": 3,
"name": "AppName",
"description": "",
"version": "0.1.0",
"permissions": [
"storage",
"scripting",
"tabs"
],
"background": {
"service_worker": "background.js"
},
"host_permissions": ["https://*/*"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["script.js"]
},
{
"matches": ["https://www.netflix.com/*"],
"js": ["script.js", "monitorNetflix.js"]
},
{
"matches": ["https://extensionpay.com/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}
],
"action": {
"default_icon": "icon.png"
},
"icons": {
"128": "icon.png"
}
}
Here is how I enter the script.js:
chrome.storage.sync.get(["authenticated"], function(result) {
console.log('Value currently is ' + result.authenticated);
if (result.authenticated) {
monitorNetflix();
}
});
const monitorNetflix = (function () {
...
}