英文:
console.log override not working in Tampermonkey
问题
以下是您要翻译的内容:
我制作了以下的Tampermonkey用户脚本进行测试,它应该按照注释的方式工作,但实际上并没有。
// ==UserScript==
// @name 测试:覆盖console.log
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 尝试掌控世界!
// @author 您
// @match https://example.com
// @run-at document-idle
// @noframes
// ==/UserScript==
var consoleLog = console.log;
var logs = [];
console.log = function(message) { // 覆盖console.log函数以捕获输出
logs.push(message); // 将消息存储在logs数组中
consoleLog.apply(console, ["[Test App] ", ...arguments]); // 调用原始的console.log函数
};
console.log('Hello, world!'); // 这将记录`[Test App] Hello, world!`
// console.log = consoleLog; // 恢复原始的console.log函数
console.log(logs); // 这将记录`["Hello, world!"]`
因此,前往https://example.com/
,打开开发工具控制台选项卡,我看到:
Hello, world!
[]
但实际上,应该是以下内容之一:
[Test App] Hello, world!
["Hello, world!"]
事实是,它在Node.js或Chrome开发工具控制台上正常工作,所以我对这种奇怪的行为感到困惑。那么,如何修复?谢谢。
英文:
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:
[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.
答案1
得分: 1
尝试 unsafewindow.console.log('打印我的数据')
我还附上了参考链接 tamper monkey unsafewindow ref
英文:
try unsafewindow.console.log('print my data')
i am also attaching the link for reference tamper monkey unsafewindow ref
答案2
得分: 0
好的,以下是已经翻译好的部分:
// ==UserScript==
// @name 测试:覆盖 console.log
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 尝试接管世界!
// @author 你
// @match https://example.com
// @run-at document-idle
// @noframes
// @grant unsafeWindow
// ==/UserScript==
var consoleLog = unsafeWindow.console.log;
var logs = [];
unsafeWindow.console.log = function(message) {
logs.push(message);
consoleLog.apply(unsafeWindow.console, ["[测试应用] ", ...arguments]);
};
unsafeWindow.console.log('你好,世界!');
// unsafeWindow.console.log(logs);
Tampermonkey 环境运行在一种“沙盒”中,某些行为可能与常规 JavaScript 上下文不同。
这里的思路是使用 unsafeWindow 引用页面上下文中的实际全局窗口对象,而不是 Tampermonkey 沙盒版本的 window。
另外,一个注意事项:不应该执行 console.log(logs);
,因为它会陷入日志调用本身的循环中。在开发者工具控制台中只需输入 logs
就足够了。
英文:
Well, I've solved the issue. Here's the revised script:
// ==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
// @grant unsafeWindow
// ==/UserScript==
var consoleLog = unsafeWindow.console.log;
var logs = [];
unsafeWindow.console.log = function(message) {
logs.push(message);
consoleLog.apply(unsafeWindow.console, ["[Test App] ", ...arguments]);
};
unsafeWindow.console.log('Hello, world!');
// unsafeWindow.console.log(logs);
> The Tampermonkey environment operates in a sort of "sandbox", and some behaviors can be different from the regular JavaScript context.
> The idea here is to use unsafeWindow to reference the actual global window object in the page context, rather than the Tampermonkey sandboxed version of window.
Also, a side note: I shouldn't do console.log(logs);
, since it would be trapped to the loop of the log call itself. Just logs
in the devtools console is enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论