How to use tabs.executeScript with reaction

In one of my components, when the component is loaded, I want my extension to paste the script into the current tab in which the extension is running. The script essentially gets the source code and saves it as a string.

So, in mine, componentWillMountI tried to enter a script as follows ...

componentWillMount() {

    var result = '';
    chrome.tabs.executeScript(null, {
        file: './js/scripts/getPageSource.js'
     }, function() {
        // If you try and inject into an extensions page or the webstore/NTP you'll get an error
        if (chrome.runtime.lastError) {
            result = 'There was an error injecting script : \n' + chrome.runtime.lastError.message;

        }
    });
}

The path to my script ./js/scripts/getPageSource.jsrefers to my file index.html, which is used by my responsive application.

I get the error file was not found, so I changed the path relative to the component, but I still could not find the file.

I think maybe this is the wrong way to paste a script into an open tab with a reaction, is there a better way to do this?

EDIT

Here is my file manifest.json...

{
  "name": "Get pages source",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_popup": "src/index.html"
  },
  "permissions": ["tabs", "<all_urls>"]
}

./js/scripts/getPageSource.js src,

+2
1

, manfest.json.

MDN:

Firefox URL- URL- . Chrome URL- URL- .

manifest.json, , file :

file: 'src/js/scripts/getPageSource.js'
+3

Source: https://habr.com/ru/post/1660364/


All Articles