javascript - Debugging Web Workers in Safari Web Inspector - Stack Overflow

Chrome's Dev Tools are great for debugging web workers as I can "browse" into that JavaS

Chrome's Dev Tools are great for debugging web workers as I can "browse" into that JavaScript environment and set break points. Even the console works as expected.

On Safari, it is a pletely different story. console.log from the web worker doesn't even print in the console. I see the worker script loaded and I put a break point on it, but it doesn't break. I don't even see the scripts that were loaded with importScripts.

How can I use Safari's Web Inspector to troubleshoot problems?

Not that I think it matters, but I'm using Safari 8.

Chrome's Dev Tools are great for debugging web workers as I can "browse" into that JavaScript environment and set break points. Even the console works as expected.

On Safari, it is a pletely different story. console.log from the web worker doesn't even print in the console. I see the worker script loaded and I put a break point on it, but it doesn't break. I don't even see the scripts that were loaded with importScripts.

How can I use Safari's Web Inspector to troubleshoot problems?

Not that I think it matters, but I'm using Safari 8.

Share Improve this question asked Jun 5, 2015 at 12:23 Daniel A. WhiteDaniel A. White 191k49 gold badges379 silver badges466 bronze badges 7
  • 1 Is there a reason you need to use Safari? Are you investigating a Safari specific problem? – Halcyon Commented Jun 5, 2015 at 12:30
  • 2 My pany's product supports Safari (and all major browsers) and we want to give customers and support engineers the ability to troubleshoot problems, no matter what web browser. – Daniel A. White Commented Jun 5, 2015 at 12:35
  • 3 Chrome, FireFox, Opera, IE, Edge; All show messages from web workers in the console. Safari? "Ha! we know better, you don't need that.." – muttonUp Commented Jul 18, 2015 at 13:19
  • All browser look fine, only Safari has problem, that is why need to debug specifically in Safari. – Kyaw Tun Commented Sep 15, 2016 at 7:44
  • @KyawTun read my above ment – Daniel A. White Commented Sep 15, 2016 at 12:08
 |  Show 2 more ments

2 Answers 2

Reset to default -1

Insert the debugger; code in your source

Usage: Insert it anywhere you want to add a breakpoint and when developer console is open automatically execution will pause at that line

var a = 50;
a = a + 5;
debugger; //--> execution is paused here
a = a - 5;

For more info see the Debugger Documentation on mozilla

In lieu of console.log, you can use postMessage. postMessage should allow you to send debug messages to the safari console.

Here is a great example on how to do that, I pasted the main idea below:

//
// In the Main thread
//
var worker = new Worker('/path/of/webworker/code.js')
worker.onmessage = function (e) {
  var result = JSON.parse(e.data);
  if(result.type == 'debug') {
    console.log(result.msg);
  } else if(result.type == 'response') {
    // ... use result.answer ...
  }
}


//
// In the WebWorker
//
function debug(msg) {                                                           
  postMessage(JSON.stringify({type:'debug',msg:msg}));                          
}

onmessage = function (e) {
  var inputData = e.data;
  // work on input data
  debug('Working OK');
  // work some more
  // ...
  postMessage(JSON.stringify({type:'response', answer:42}));
};

If you don't want to play around with postMessage though, David Flanagan made a wrapper for it here that should allow you to at least do debugging with console.log

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339393a4623254.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信