console.log覆盖在Tampermonkey中不起作用

huangapple go评论189阅读模式
英文:

console.log override not working in Tampermonkey

问题

以下是您要翻译的内容:

我制作了以下的Tampermonkey用户脚本进行测试,它应该按照注释的方式工作,但实际上并没有。

  1. // ==UserScript==
  2. // @name 测试:覆盖console.log
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 尝试掌控世界!
  6. // @author 您
  7. // @match https://example.com
  8. // @run-at document-idle
  9. // @noframes
  10. // ==/UserScript==
  11. var consoleLog = console.log;
  12. var logs = [];
  13. console.log = function(message) { // 覆盖console.log函数以捕获输出
  14. logs.push(message); // 将消息存储在logs数组中
  15. consoleLog.apply(console, ["[Test App] ", ...arguments]); // 调用原始的console.log函数
  16. };
  17. console.log('Hello, world!'); // 这将记录`[Test App] Hello, world!`
  18. // console.log = consoleLog; // 恢复原始的console.log函数
  19. console.log(logs); // 这将记录`["Hello, world!"]`

因此,前往https://example.com/,打开开发工具控制台选项卡,我看到:

  1. Hello, world!
  2. []

但实际上,应该是以下内容之一:

  1. [Test App] Hello, world!
  2. ["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.

  1. // ==UserScript==
  2. // @name Test: overriding console.log
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://example.com
  8. // @run-at document-idle
  9. // @noframes
  10. // ==/UserScript==
  11. var consoleLog = console.log;
  12. var logs = [];
  13. console.log = function(message) { // Override the console.log function to capture the output
  14. logs.push(message); // Store the message in the logs array
  15. consoleLog.apply(console, ["[Test App] ", ...arguments]); // Call the original console.log function
  16. };
  17. console.log('Hello, world!'); // this would log `[Test App] Hello, world!`
  18. // console.log = consoleLog; // Restore the original console.log function
  19. console.log(logs); // this would log `["Hello, world!"]`

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

  1. Hello, world!
  2. []

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

  1. [Test App] Hello, world!
  2. ["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

英文:

console.log覆盖在Tampermonkey中不起作用

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

答案2

得分: 0

好的,以下是已经翻译好的部分:

  1. // ==UserScript==
  2. // @name 测试:覆盖 console.log
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description 尝试接管世界!
  6. // @author 你
  7. // @match https://example.com
  8. // @run-at document-idle
  9. // @noframes
  10. // @grant unsafeWindow
  11. // ==/UserScript==
  12. var consoleLog = unsafeWindow.console.log;
  13. var logs = [];
  14. unsafeWindow.console.log = function(message) {
  15. logs.push(message);
  16. consoleLog.apply(unsafeWindow.console, ["[测试应用] ", ...arguments]);
  17. };
  18. unsafeWindow.console.log('你好,世界!');
  19. // unsafeWindow.console.log(logs);

Tampermonkey 环境运行在一种“沙盒”中,某些行为可能与常规 JavaScript 上下文不同。

这里的思路是使用 unsafeWindow 引用页面上下文中的实际全局窗口对象,而不是 Tampermonkey 沙盒版本的 window。

另外,一个注意事项:不应该执行 console.log(logs);,因为它会陷入日志调用本身的循环中。在开发者工具控制台中只需输入 logs 就足够了。

英文:

Well, I've solved the issue. Here's the revised script:

  1. // ==UserScript==
  2. // @name Test: overriding console.log
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description try to take over the world!
  6. // @author You
  7. // @match https://example.com
  8. // @run-at document-idle
  9. // @noframes
  10. // @grant unsafeWindow
  11. // ==/UserScript==
  12. var consoleLog = unsafeWindow.console.log;
  13. var logs = [];
  14. unsafeWindow.console.log = function(message) {
  15. logs.push(message);
  16. consoleLog.apply(unsafeWindow.console, ["[Test App] ", ...arguments]);
  17. };
  18. unsafeWindow.console.log('Hello, world!');
  19. // 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.

huangapple
  • 本文由 发表于 2023年6月22日 16:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定