9

I want to run a content script on an iframe with a chrome-extension:// URL. I added a line to my manifest.json that I copied out of the documentation http://code.google.com/chrome/extensions/match_patterns.html

 chrome-extension://*/*

But when I reload my extension I get an alert:

Could not load extension from '/work/sirius/extension'. 
Invalid value for 'content_scripts[2].matches[0]': Invalid scheme.

Any idea how to get this to worK?

johnjbarton
  • 1,857
  • 1
  • 13
  • 12
  • As noted in @Konstantin Smolyanin's answer below, the documention now says nothing about ability to inject content scripts to `chrome-extension:` pages. – Dave Land Sep 24 '15 at 18:22
  • 1
    Possible duplicate of [Can I modify chrome://extensions/ page with JavaScript(user script)?](https://stackoverflow.com/questions/6148487/can-i-modify-chrome-extensions-page-with-javascriptuser-script) – Zach Saucier Jun 04 '18 at 19:04

4 Answers4

13

No. Only ftp:, file:, http: and https: can be matched by a content script declaration.

Invalid URL patterns at any of the matches and exclude_matches fields are rejected (generating an error when trying to load the extension).

Invalid patterns at the permissions option in the manifest file are ignored.

If you want to run a script on a tab from your extension, use chrome.extension.getViews in your background script. Even better, design your extension's pages such that they effectively communicate with each other (example).

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • I want to run a content script on an iframe in a web page. chrome.extension.getViews() allows scripts on extensions, not web pages. – johnjbarton Apr 18 '12 at 16:00
  • I am implementing a postMessage based communications mechanism to the iframe, but the iframe is not my code. So I want the content_scripts to allow me to inject the postMesssage() endpoint. – johnjbarton Apr 18 '12 at 16:03
  • The above link to the 'permissions option in the manifest file' lead me to investigate using programmatic script injection. Unfortunately this won't work either: the web app hosting the chrome-extension:// iframe can't run the extension code required to inject into its iframe. Perhaps I can write a separate extension to programmatically inject when the tab has a matching URL. – johnjbarton Apr 18 '12 at 18:12
  • @johnjbarton You'r right. By not seeing an error when trying the permissions option, I assumed that it's a valid value. Upon further investigation, I discovered that no errors are generated for invalid patterns. – Rob W Apr 18 '12 at 20:14
  • I tried the programmatic injection from an extension. This also fails because the iframes themselves are added programmatically and chrome.tabs.executeScript() even with "allFrames:true" only fires during the html parsing. Perhaps a content script on the web app that watches for iframes matching the chrome-extension:// url, then messages the background page to call chrome.tabs.executeScript(). But that is over my limit for silly-tricky :-( – johnjbarton Apr 18 '12 at 21:23
  • @johnjbarton FYI, I had tested ways to execute code on arbitrary `chrome-*//*` pages, and failed. This observation was included in my updated answer. If you have control over the pages containing the iframe, and access to the iframes are not restricted by the cross-origin policy, use a technique similar to this one: [How can I detect keyboard events in Gmail](http://stackoverflow.com/a/9439525/938089?how-can-i-detect-keyboard-events-in-gmail). – Rob W Apr 19 '12 at 08:42
8

I'm having the exact same problem, look at the API http://code.google.com/chrome/extensions/match_patterns.html it says clearly that they accept chrome-extension://*/* yet they don't.

They need to update the API so as not to confuse people.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Joel Blum
  • 7,750
  • 10
  • 41
  • 60
7

It seems that Chrome authors have silently removed the ability for content scripts to be injected into chrome-extension: pages. Documentation still says that it works and even contains examples with chrome-extension: scheme but actually it doesn't work. So now only http:, https: and ftp: work "from the box" and file: can work if user of your extension has enabled this on Extensions (chrome://extensions/) page.

Update: now documentation referred above is updated and says nothing about ability to inject content scripts to chrome-extension: pages.

Konstantin Smolyanin
  • 17,579
  • 12
  • 56
  • 56
0

You can inject js to your iframe html(chrome-extension: pages) without declaring it in manifast.json. The injected js can visit Chrome APIs directly.

iframe.html:

<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
...
</body>
<script src="iframe.js"></script>
</html>

iframe.js:

console.log(chrome); // {loadTimes: ƒ, csi: ƒ, …}
ran meimei
  • 49
  • 2