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.