0

I am creating an iframe within one of my chrome extension's pages - unfortunate the source page can be noisy, and cause sourcemap warnings, or lead to errors being logged (luckily these don't leak out and show as extension errors, but they're still noisy).

I'd like to keep the console logging focused on the extension's output directly. Is there a way I can turn off console logging in the iframe? I am inserting a content script that among other things does the following, but I think all of the logging has already happened by the time the script evaluates.


       // suppress console messages from within the iframe
        window.console.log = function() {};
        window.console.info = function() {};
        window.console.warn = function() {};
        window.console.error = function() {};

        // Disable network warnings and devtools messages
        if (window.__REACT_DEVTOOLS_GLOBAL_HOOK__) {
            window.__REACT_DEVTOOLS_GLOBAL_HOOK__.isDisabled = true;
            window.__REACT_DEVTOOLS_GLOBAL_HOOK__.showTabBar = false;
        }

        // Disable source map loading warnings
        if (window.WebInspector && window.WebInspector.ConsoleModel) {
            window.WebInspector.ConsoleModel.prototype.addMessage = function() {};
        }

UPDATE, per discussion below:

I split this into two scripts.

Before populating the iframe I inject the scripts as follows:

            .then(() => this.unregisterIfExists(this.muterScriptId))
            .then(() => this.unregisterIfExists(this.scraperScriptId))
            .then(() => chrome.scripting.registerContentScripts([
                {
                    id: this.muterScriptId,
                    js: [ "iframemuter.js" ],
                    allFrames: true,
                    persistAcrossSessions: false,                  
                    matches: this.urlToWildcardPatterns(url),
                    world: "MAIN",
                    runAt: "document_start"
                },
                {
                    id: this.scraperScriptId,
                    js: [ "receiver.js" ],
                    allFrames: true,
                    persistAcrossSessions: false,                   
                    matches: this.urlToWildcardPatterns(url),
                    runAt: "document_idle"
                }
            ]))
    ```

The muter script has a class with the above console muting, and the class is created. The scraper script has a constructor which ends with `console.error("suppression failed");`

This console error is logged every time.
user717847
  • 695
  • 1
  • 6
  • 16
  • You need to run this code in page context (MAIN world), [more info](/a/9517879). And specify `"run_at": "document_start"` for the content script. – wOxxOm May 19 '23 at 16:20
  • @wOxxOm it seems I can't do both `world: MAIN` and also use `chrome.runtime.onMessage` in the content script - is that right? – user717847 May 20 '23 at 21:08
  • Yes. Either use two scripts and communicate between them via CustomEvent or use [external messaging](https://developer.chrome.com/extensions/messaging#external-webpage) in a single MAIN script. – wOxxOm May 21 '23 at 04:52
  • I cannot seem to make this work reliably at all. I split the two scripts, so one now has all of the suppressions. It's loaded as a content script first at document_start and MAIN world. The second script (which does the actual scraping) is loaded at document_idle and default (which is I guess isolated) world. I have a console.error in the constructor of the second script, and it is not suppressed. I guess this makes sense, since the world of the scraper is isolated? – user717847 May 27 '23 at 11:58
  • Done - added an update. – user717847 May 27 '23 at 12:55
  • Your script runs before the site adds the hooks so both `if` conditions are false. Instead, try Object.defineProperty to add a setter for these two props. Inside the setter you'll patch the value and call Object.defineProperty again to override this setter with the value. – wOxxOm May 27 '23 at 13:46
  • I will try that later today and update - but what about the console.error? Shouldn't it be redirecting? Or does the dom add console hooks later in the lifecycle? – user717847 May 27 '23 at 14:21
  • I can't say without seeing all the relevant code ([MCVE](/help/mcve)) and/or inspecting the original site. – wOxxOm May 27 '23 at 18:43
  • Well, I can verify that the scripts are both running - they log to the console. There's really not much more code to it; the muter has my original code in the constructor, and the scraper has a console.error which gets output. I got completely lost on your Object.defineProperty answer, and so far I haven't found a good code snippet, but I will do more digging. – user717847 May 28 '23 at 22:57
  • Actually a little more nuance: console.error within the muter IS suppressed, but errors from within the page still are not suppressed. console.error within the scraper is not suppressed, perhaps because they are in different worlds? – user717847 May 29 '23 at 00:10
  • Observation on this: I added a body.style.transform to the start of the muter; it only applies on around 30% of pages if I insert on document_end. Either the rest of the pages are somehow clearing my transform (unlikely), or the content script is being wiped through a reload or similar. I'll debug further. – user717847 Jun 01 '23 at 12:57

0 Answers0