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