最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Console.log not working inside of setInterval for Tampermonkey - Stack Overflow

programmeradmin4浏览0评论

I've used setInterval() plenty of times in typical scripts, but not with userscripts. For some reason, console.log() isn't working, but only inside of the setInterval. The alert, however, is working. Any ideas..? Should I not be using console.log?

To clarify, the first console.log("Started!"); does in fact print started.

(function() {
    console.log("Started!");
    setInterval(function(){ findAndReplace();}, 3000);
})();

function findAndReplace() {
    alert("hi");
    console.log("Hey");
} 

I've used setInterval() plenty of times in typical scripts, but not with userscripts. For some reason, console.log() isn't working, but only inside of the setInterval. The alert, however, is working. Any ideas..? Should I not be using console.log?

To clarify, the first console.log("Started!"); does in fact print started.

(function() {
    console.log("Started!");
    setInterval(function(){ findAndReplace();}, 3000);
})();

function findAndReplace() {
    alert("hi");
    console.log("Hey");
} 
Share Improve this question edited Mar 7, 2015 at 21:42 AstroCB 12.4k20 gold badges58 silver badges74 bronze badges asked Mar 7, 2015 at 16:59 Christopher WirtChristopher Wirt 1,1171 gold badge10 silver badges21 bronze badges 7
  • Hmm.. seems to work here: jsfiddle.net/seyenaz/ss20m0qt – Omri Aharon Commented Mar 7, 2015 at 17:33
  • It works in IE, but not FireFox. – Steve Wellens Commented Mar 7, 2015 at 17:38
  • 1 This works perfectly fine in Tampermonkey. Something you are not showing us is the problem. Complete (but concise to the problem) script? This includes the metadata block. Chrome and TM versions? Non-standard TM settings? – Brock Adams Commented Mar 7, 2015 at 21:16
  • 3 Also be aware that some few web sites do trash the console functions; I've seen this on about 3 sites, IIRC. The console.log("Started!") would be firing (just) before that happens, if that is your case. – Brock Adams Commented Mar 7, 2015 at 21:21
  • 3 @BrockAdams this was correct, Twitter overrides this. I ended up just putting console.log = console.__proto__.log at the start of my functions and it works just fine now. – Christopher Wirt Commented Mar 7, 2015 at 22:00
 |  Show 2 more comments

3 Answers 3

Reset to default 18

The answer supplied by OPer didn't work for me, it seems that's no longer an option. Grant GM_log to your script and use the GM_log function as a substitute for console.log.

Add this to the userscript header:

// @grant GM_log

Use this in your code:

GM_log('<my debug message>');

Apparently, Twitter overrides console.log. The "solution" is to put console.log = console.__proto__.log at the top of my functions.

To your tamermonkey script header add

// @grant GM_log

then console.log("hello world"); should work

发布评论

评论列表(0)

  1. 暂无评论