3

In one of my components, when the component is loaded, I want my extension to inject a script into the current tab that the extension is run on. The script essentially gets the source code and save it as a string.

So, in my componentWillMount I tried to inject the script in the following way...

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 of my script, ./js/scripts/getPageSource.js is relative to my index.html file that is being used by my react app.

I get the error file is not found, so I changed the path relative to the component, but I still go file not found.

I'm thinking maybe this is not the right way to inject a script into an open tab with react, is there a better way of doing this?

EDIT

Here is my manifest.json file...

{
  "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>"]
}

My path ./js/scripts/getPageSource.js is located inside my src folder being referenced above

buydadip
  • 8,890
  • 22
  • 79
  • 154
  • @Makyen If you need me to add the manifest file and the script being called, I can do so. But my main issue is I am trying to inject a script and it is not being called at all. As far as I can tell, you don't really need to know what the script does do you? – buydadip Nov 07 '16 at 21:05
  • @Makyen ok I added it – buydadip Nov 07 '16 at 21:09

1 Answers1

4

Use a path that is relative to your manfest.json file.

MDN states:

In Firefox, relative URLs are resolved relative to the current page URL. In Chrome, these URLs are resolved relative to the add-on's base URL.

From your manifest.json, it looks like your file should be:

file: 'src/js/scripts/getPageSource.js'
Makyen
  • 31,849
  • 12
  • 86
  • 121