<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Andy Li’s Blog - HTML5</title><link href="https://blog.onthewings.net/" rel="alternate"></link><link href="http://feeds.feedburner.com/tag/html5/feed/atom.xml" rel="self"></link><id>https://blog.onthewings.net/</id><updated>2010-04-13T02:41:00+08:00</updated><entry><title>CanvasBlock: a GM script to block canvas tag (should we…or can we?)</title><link href="https://blog.onthewings.net/2010/04/13/canvasblock-a-gm-script-to-block-canvas-tag-should-we-or-can-we/" rel="alternate"></link><published>2010-04-13T02:41:00+08:00</published><updated>2010-04-13T02:41:00+08:00</updated><author><name>Andy Li</name></author><id>tag:blog.onthewings.net,2010-04-13:/2010/04/13/canvasblock-a-gm-script-to-block-canvas-tag-should-we-or-can-we/</id><summary type="html">&lt;p&gt;&lt;span class="caps"&gt;HTML5&lt;/span&gt; is believed to be the Flash killer and will replace Flash in the
future. Eventually those Flash-haters will become canvas-haters, so a
canvas-blocking GreaseMonkey script will be useful (at least they think
it will be).&lt;/p&gt;
&lt;p&gt;I myself was making it for fun. I don’t think we should block …&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;span class="caps"&gt;HTML5&lt;/span&gt; is believed to be the Flash killer and will replace Flash in the
future. Eventually those Flash-haters will become canvas-haters, so a
canvas-blocking GreaseMonkey script will be useful (at least they think
it will be).&lt;/p&gt;
&lt;p&gt;I myself was making it for fun. I don’t think we should block any &lt;span class="caps"&gt;HTML5&lt;/span&gt;
tags anytime since it is a part of the web, just like I do not agree
people blocking Flash. What is interesting is, seems like we
cannot effectively block the canvas tag, which will be discussed later
in this post.&lt;/p&gt;
&lt;h3&gt;Features&lt;/h3&gt;
&lt;p&gt;I made the script blocks not only canvas tag but also video tag and
audio tag. It is because Flash is going to be replaced by all the three
tags but not only canvas. Video and audio can be as annoying as canvas.&lt;/p&gt;
&lt;p&gt;Like those Flash-blockers, the blocked contents are replaced by a line
of text, “Click to show xxxx”. You may click on it to get back the
blocked content.&lt;/p&gt;
&lt;p&gt;CanvasBlock is tested on Firefox 3.6 on Mac, Firefox 3.5 on Ubuntu and
Chrome on Ubuntu. Other platforms should works too.&lt;/p&gt;
&lt;h3&gt;Implementing the script&lt;/h3&gt;
&lt;p&gt;I used haXe instead of writing pure &lt;span class="caps"&gt;JS&lt;/span&gt;. No big reason here, just to see
if it is possible. Turn out there is no big difference from using &lt;span class="caps"&gt;JS&lt;/span&gt;,
you just need to copy all the meta data to the compiled &lt;span class="caps"&gt;JS&lt;/span&gt; file. And the
events listener need to be assigned by &lt;code&gt;addEventListener&lt;/code&gt; instead of
something like &lt;code&gt;onclick = function ...&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Detecting &lt;span class="caps"&gt;HTML5&lt;/span&gt; tags is easier than detecting Flash objects. Firstly,
the tags are standardized meaning you can simply use
&lt;code&gt;getElementsByTagName&lt;/code&gt;. For Flash objects you need to check the params
of the &lt;code&gt;object&lt;/code&gt; and the &lt;code&gt;embed&lt;/code&gt; tags, which is a bit troublesome.
Secondly, currently no one (at least not much ppl) is creating canvas
tags on the fly, unlike for Flash the standard way is to use swfobject.
So I can detect the tags once the &lt;span class="caps"&gt;DOM&lt;/span&gt; is ready, no need to wait for the
execution of the page’s &lt;span class="caps"&gt;JS&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Replacing the tags and saving them for later retrieval is easy too.
Using normal &lt;span class="caps"&gt;JS&lt;/span&gt; method will work.&lt;/p&gt;
&lt;h3&gt;The tags are blocked but…&lt;/h3&gt;
&lt;p&gt;The script is functional and I went to do some more testing. Blocking
video and audio seems ok, but there is some problem of blocking canvas.&lt;/p&gt;
&lt;h4&gt;Possibility of Freezing/Crashing&lt;/h4&gt;
&lt;p&gt;I went to &lt;a href="http://www.chromeexperiments.com/"&gt;ChromeExperiments&lt;/a&gt; and randomly picked &lt;a href="http://www.chromeexperiments.com/detail/asteroids-game/"&gt;one&lt;/a&gt;. You know
what? The browser froze.&lt;/p&gt;
&lt;p&gt;So, look like after I blocked the canvas, the &lt;span class="caps"&gt;JS&lt;/span&gt; on the page which is
used to draw thing on the canvas originally, is still there trying to
work on the canvas. And this become a big problem since I don’t
know exactly which &lt;span class="caps"&gt;JS&lt;/span&gt; function is going to work on
canvas, because different web page have different functions, so no way I
can work on that. You can completely disable &lt;span class="caps"&gt;JS&lt;/span&gt;, but this is not a
CanvasBlock should do.&lt;/p&gt;
&lt;p&gt;Browser froze and an timeout message was shown after blocking canvas:
&lt;img alt="Browser freezed and an timeout message was shown after blocking canvas" src="/files/2010/errorAfterBlockingCanvas.png"/&gt;&lt;/p&gt;
&lt;p&gt;However, I have tested some more web page and they are ok with the
blocking script. So maybe only some complex &lt;span class="caps"&gt;JS&lt;/span&gt; will have this problem.
But anyway the possibility of freezing a browser is not a good thing.&lt;/p&gt;
&lt;h4&gt;No significant reduction on &lt;span class="caps"&gt;CPU&lt;/span&gt; usage&lt;/h4&gt;
&lt;p&gt;The remaining &lt;span class="caps"&gt;JS&lt;/span&gt; script  problem reminded me to test on the &lt;span class="caps"&gt;CPU&lt;/span&gt; usage.
And the result is, blocking canvas does not bring significant reduction
on &lt;span class="caps"&gt;CPU&lt;/span&gt; usage. See the following screenshots, which is using &lt;a href="http://www.chromeexperiments.com/detail/aquarium/"&gt;another
Chrome experiment&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;Page opened, canvas blocked. However &lt;span class="caps"&gt;CPU&lt;/span&gt; usage still rise to a
very high level:
&lt;img alt="Page opened, canvas blocked. However CPU usage still rise to a
very high level." src="/files/2010/cpuUsage-1.png"/&gt;&lt;/p&gt;
&lt;p&gt;Clicked to show back the canvas. &lt;span class="caps"&gt;CPU&lt;/span&gt; usage rose even higher,
but not much difference:
&lt;img alt="Clicked to show back the canvas. CPU usage rose even higher,
but not much difference." src="/files/2010/cpuUsage-2.png"/&gt;&lt;/p&gt;
&lt;p&gt;So what’s that mean? Again if I want to block the real &lt;span class="caps"&gt;CPU&lt;/span&gt; hog, I should
block the &lt;span class="caps"&gt;JS&lt;/span&gt; with the canvas. But again, stated above, it is not
possible to block the right function without blocking all &lt;span class="caps"&gt;JS&lt;/span&gt;.&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;Blocking canvas can only introduce little benefit on &lt;span class="caps"&gt;CPU&lt;/span&gt; usage but give
you possibility of freezing/crashing!&lt;/p&gt;
&lt;p&gt;Let’s look at Flash again. Flash can be blocked easily because
ActionScript is included in the swf. Removing the swf from the
page automatically removes the associated scripts. Also, usually &lt;span class="caps"&gt;JS&lt;/span&gt; does
not control a Flash object so it is pretty safe to kick Flash out
without dealing with the remained &lt;span class="caps"&gt;JS&lt;/span&gt;. From this point of view, seems
that Flash is more user friendly than canvas, and &lt;span class="caps"&gt;HTML5&lt;/span&gt; ads will be more
annoying than Flash ads…&lt;/p&gt;
&lt;p&gt;If you want to try, you may install the script from &lt;a href="http://userscripts.org/scripts/show/74216"&gt;CanvasBlock’s page
on userscripts.org&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You’re also welcome to get the haXe source from &lt;a href="http://github.com/andyli/CanvasBlock"&gt;CanvasBlock’s Github
repo&lt;/a&gt;.&lt;/p&gt;</content><category term="Flash"></category><category term="Haxe"></category><category term="HTML5"></category><category term="JavaScript"></category></entry></feed>