0

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 () {
...
}
SamuelC00
  • 61
  • 6
  • One of the scripts you injected uses `document`, which is impossible in the background script. If you want to use executeScript then you should put the code that uses `document` (and other DOM) inside a function e.g. in this case it'll be `function script` and `function monitorNetflix`. – wOxxOm Sep 07 '22 at 05:25
  • Thank you. I see that you answered two of my three questions I've asked in total. Perhaps you could also answer this one for me? https://stackoverflow.com/questions/73511222/react-native-with-eas-lateinit-property-descriptionadapter-has-not-been-initali – SamuelC00 Sep 08 '22 at 00:02

0 Answers0