I can't seem to make this work no matter what I do. Let me demonstrate with a very simple example extension. Here is the manifest.json
:
{
"manifest_version": 2,
"name": "Sample Of Content Script",
"description": "Changes the background of a page pink",
"version": "1.0",
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": [ "changer.js" ]
}
],
"permissions": [
"webNavigation"
],
"background": {
"scripts": [ "background.js" ]
}
}
Notice that my content_scripts
entry matches all_urls
, which (according to Google documentation) should match file:///
URLs.
The background.js
:
(function (chrome) {
'use strict';
chrome.webNavigation.onCompleted.addListener(function (details) {
chrome.tabs.sendMessage(details.tabId, {
action: 'changeBackground',
color: 'pink'
});
});
})(chrome);
And the changer.js
(content script):
(function (chrome) {
'use strict';
chrome.runtime.onMessage.addListener(function (request) {
if (request.action !== 'changeBackground') { return; }
document.body.style.background = request.color;
});
})(chrome);
This extension has been published on the Chrome Web Store so you can see the result in action:
https://chrome.google.com/webstore/detail/sample-of-content-script/bkekbfjgkkineeokljnobgcoadlhdckd
It's a pretty simple extension. Navigate to a page, and it turn's the page's background pink. Unfortunately, it doesn't work for any file:///
URLs. The changer.js
script is not loaded into the page, and nothing happens.
chrome.tabs.executeScript()
instead of putting the script into the manifest. This failed in a more obvious way, saying that I didn't request permissions to modify file:///
URLs in the manifest."file:///*/*"
to the permissions
section of manifest.json
. That seemed to workwell with chrome.tabs.executeScript()
, but the Chrome Web Store rejected the extension, saying that file:///
permissions are not allowed.content_script
section in manifest.json
and tried adding "file:///*/*
to the matches
section in the manifest. Again, this worked in a development build, but when I uploaded it to the Chrome Web Store and then installed it, it didn't work.If you open a Chrome browser window and navigate to chrome://extensions, you may notice the option "Allow access to file URLs" underneath of the LastPass extension. This option is mostly for web developers. Checking this option allows LastPass to login and fill forms for local files such as file:///C:/dev/test.html.
To inject a content script programmatically, your extension needs host permissions for the page it's trying to inject scripts into. Host permissions can either be granted by requesting them as part of your extension's manifest (see host_permissions ) or temporarily via activeTab.
Open Chrome, press Ctrl+Shift+j and it opens the JavaScript console where you can write and test your code.
Content scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.
<all_urls>
indeed covers file://
scheme, but it must be manually activated in the extensions list.
If an extension has permissions that cover file://
scheme, it will have a checkbox "Allow access to file URLs" next to "Allow in incognito". The user must enable that manually; you can help by creating a tab with preconfigured URL, after explaining the process:
chrome.tabs.create({url: "chrome://extensions/?id=" + chrome.runtime.id});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With