Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

console.log override not working in Tampermonkey

I made the following Tampermonkey userscript for testing, it’s supposed to be worked as commented, but it doesn’t.

// ==UserScript==
// @name         Test: overriding console.log
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://example.com

// @run-at       document-idle
// @noframes
// ==/UserScript==

var consoleLog = console.log;
var logs = [];
console.log = function(message) { // Override the console.log function to capture the output
  logs.push(message);  // Store the message in the logs array
  consoleLog.apply(console, ["[Test App] ", ...arguments]);  // Call the original console.log function
};
console.log('Hello, world!'); // this would log `[Test App] Hello, world!`
// console.log = consoleLog; // Restore the original console.log function
console.log(logs); // this would log `["Hello, world!"]`

So, going to https://example.com/, opens devtools console tab, I see:

Hello, world!
[]

But, it’s supposed to be something as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[Test App] Hello, world!
["Hello, world!"]

The fact is that it works fine on node.js or on the chrome devtools console, so I’m confused with the weird behavior. So, how to fix? Thanks.

>Solution :

enter image description here

try unsafewindow.console.log('print my data') i am also attaching the link for reference tamper monkey unsafewindow ref

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading