Search code examples
google-chrome-extensioncontent-scriptmhtml

Chrome Content script injection in MHTML files


It seems the Chrome does not inject content script into local files that are of type MHTML. They might do this for security reason. They don't allow you to download files that have MHTML extension either. So that makes me suspicious.

My content script gets injected properly if the local file type is HTML.

Here is my manifest:

"content_scripts": [
    {
        "matches": [ "http://*/*", "https://*/*", "file://*/*", "<all_urls>" ],
        "run_at": "document_start",
        "js": [
            "js/contentscript.js"
        ]
    }
],

"permissions": [ "tabs", "http://*/*", "https://*/*"],

In the extension management page I also checked:

[x] Allow Access to file URLs

And finally the error I get:

test1.mhtml:1 Blocked script execution in 'file:///Users/test/Downloads/test1.mhtml' 
because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

Is there anyway to work around this and get my script injected in .mhtl file?

Update: Here is a simple test extension that shows script injection and a test mhtml file. Make sure you check this check box in extension management page. [x] Allow Access to file URLs

Update 2: Found it. It is a chrome bug https://code.google.com/p/chromium/issues/detail?id=452901

Update 3: so it looks like that it works but chrome debugger just does not show the content script files in the UI when the file type is MHTML.


Solution

  • The content script is added via the standard declaration, for example:

    "content_scripts": [
        {
            "matches": [ "<all_urls>" ],
            "run_at": "document_end",
            "js": ["content.js"]
        }
    ],
    

    Notes:

    • it won't be shown in Chrome devtools -> Sources -> Content scripts panel which seems a bug.
    • MHTML has some restrictions so certain features may not work, use console.log everywhere.