Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web worker console.log

Is it just me, or is console.log() too much to ask for from HTML5 web workers?

I know that manipulating the DOM is blocked because it is potentially dangerous, but is there really any possibility that console.log() could be maliciously exploited by a multithreaded worker?

like image 602
ejang Avatar asked Aug 31 '11 01:08

ejang


People also ask

How do I check web worker?

With chrome, you can navigate to chrome://inspect/#workers. Chrome inspect web workers. There you go, you have the developer tools in the context of the WebWorker. Now you can debug the scripts or see what is going on in the network tab.

How do I debug a Sharedworker?

For shared worker, you would need to go to chrome://inspect/#workers. Select "Shared workers" on the left panel. You would be able to see a list of shared workers if you have any running. You can click "inspect", which will open a new console for you to debug.

What are web workers used for?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.

What is the difference between service worker and web worker?

Unlike web workers, service workers allow you to intercept network requests (via the fetch event) and to listen for Push API events in the background (via the push event). A page can spawn multiple web workers, but a single service worker controls all the active tabs under the scope it was registered with.


2 Answers

Agreed things would be a lot nicer, but it's not too hard to hack up a primitive console.log using postMessage. David Flanagan has a nice wrapper here.

like image 67
ebidel Avatar answered Sep 25 '22 00:09

ebidel


Just wanted to post that console.log is now possible at least within the Chrome Browser.

I do not know which version it was added but 35.0.1916.153 m has it.

Limitation

There is a small limitation with it though, It can only output primitives (strings, numbers, booleans) sometimes single dimension arrays.

And it can only take the first argument within the console log.

Normal Console log:

console.log("status:", _status); // status: working console.log({ status: _status }); // { "status": working } 

Worker Console log:

console.log("status:", _status); // status: console.log({ status: _status }); // [object Object] 

You could use console.log(JSON.stringify({ status: _status })); but this would not handle circular referencing objects and will not output in a pretty/easy to read objects.

Update: You can get pretty print with stringify by doing console.log(JSON.stringify(someObject, null, " "));.

like image 26
WORMSS Avatar answered Sep 24 '22 00:09

WORMSS