Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get content_scripts to execute in file:/// URLs in a Chrome extension

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.

Extra info

  • It actually seems to work just fine when running as an Unpacked extension in Developer mode. Pages loaded from the file system turn pink.
  • I tried using 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.
  • I added "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.
  • I reverted to a 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.
like image 400
danBhentschel Avatar asked Nov 06 '15 21:11

danBhentschel


People also ask

How do I enable URL access in Chrome?

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.

How do I add content script to Chrome extension?

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.

How do I run a script in Chrome?

Open Chrome, press Ctrl+Shift+j and it opens the JavaScript console where you can write and test your code.

What are Content_script files does in Chrome extension?

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.


1 Answers

<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});
like image 147
Xan Avatar answered Sep 29 '22 20:09

Xan