<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zethrae.us/blog &#187; javascript</title>
	<atom:link href="http://zethrae.us/blog/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://zethrae.us/blog</link>
	<description>using technology to facilitate awesome.</description>
	<lastBuildDate>Thu, 19 Jan 2012 05:45:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Playing with user scripts</title>
		<link>http://zethrae.us/blog/2010/playing-with-user-scripts/</link>
		<comments>http://zethrae.us/blog/2010/playing-with-user-scripts/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 23:07:09 +0000</pubDate>
		<dc:creator>zethraeus</dc:creator>
				<category><![CDATA[web_dev]]></category>
		<category><![CDATA[4chan]]></category>
		<category><![CDATA[cs016]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[userscript]]></category>

		<guid isPermaLink="false">http://zethrae.us/blog/?p=201</guid>
		<description><![CDATA[I&#8217;m taking a rather fun algorithms course with a website that reddit loves but I do not. I decided to turn this dire, lolcat-overloaded, situation into an opportunity to practice some javascript (without jQuery) and to write my first userscript. &#8230; <a href="http://zethrae.us/blog/2010/playing-with-user-scripts/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m taking a rather fun algorithms course with a <a href="http://www.cs.brown.edu/courses/cs016/">website</a> that <a href="http://www.reddit.com/r/funny/comments/azo5c/weve_all_had_that_one_really_eccentric_professor/">reddit loves</a> but I do not. I decided to turn this dire, lolcat-overloaded, situation into an opportunity to practice some javascript (without jQuery) and to write my first userscript.</p>

<p>User scripts are javascript files with the extension &#8216;.user.js&#8217; that are executed client-side to add functionality to a web page. They are natively supported by chrome and opera and are supported via extensions in most other browsers of any note. (Some include Greasemonkey for Firefox, GreaseKit for Safari and Trixie for IE).</p>

<p>I succeeded in reskinning the whole website which was pretty fun. There seems to be a limit to the usefulness of userscripts in that support for the &#8216;@run-at document-start&#8217; flag which I believe should make the script run before any scripts on the page do, is not supported by most browsers. It&#8217;s not all that useful a flag anyway as I imagine you&#8217;d often want the script to run after the DOM has been written so that you can work with the content expected to be on the page. In this case however it would be useful as I attempt to strip out a script on the course page that redirects 1% of all hits to a rickroll (sigh). As such the script still runs before the userscript and so the rickrolling still happens.</p>

<p>I might also put some more time into the replacement style sheet some time soon.</p>

<p style="text-align: center;"><a href="http://zethrae.us/blog/wp-content/uploads/cs16.png"><img class="size-medium wp-image-206 aligncenter" title="cs16" src="http://zethrae.us/blog/wp-content/uploads//cs16-254x300.png" alt="CS16 course site" width="254" height="300" /></a></p>

<p style="text-align: center;">Original<a href="http://zethrae.us/blog/wp-content/uploads//cs16.user_.js.png"><img class="size-medium wp-image-207 aligncenter" title="cs16.user.js" src="http://zethrae.us/blog/wp-content/uploads//cs16.user_.js-300x245.png" alt="CS16 course site with userscript and restyle" width="300" height="245" /></a></p>

<p style="text-align: center;">Reshaped and restyled</p>

<p style="text-align: left;">Code after the break.<span id="more-201"></span></p>

<h3 style="text-align: left;">Final Code:</h3>

<p><pre>﻿// ==UserScript==
// @name           !cs16chan
// @namespace      http://zethrae.us
// @description    Strips mematic content from the cs16 course website
// @include        http://cs.brown.edu/courses/cs016/*
// @include        http://www.cs.brown.edu/courses/cs016/*
// @run-at document-start
// ==/UserScript==</p>

<p>(function() {
 //the following function is from http://snipplr.com/view/1696/get-elements-by-class-name/
 //the internet claims that document.getElementsByClassName() is supported in firefox 3.
 //this doesn't seem to be the case on the CIT machines, so I guess this will have to do.
 //and yes. if i was writing this again, I'd just include and use jquery.
 function getElementsByClassName(classname, node) {
 if(!node) node = document.getElementsByTagName("body")[0];
 var a = [];
 var re = new RegExp('&#92;b' + classname + '&#92;b');
 var els = node.getElementsByTagName("*");
 for(var i=0,j=els.length; i&lt;j; i++)
 if(re.test(els[i].className))a.push(els[i]);
 return a;
 }</p>

<p>function insertAfter(parent, node, referenceNode) {
 parent.insertBefore(node, referenceNode.nextSibling);
}</p>

<p>function strip_css_js(){
 var jsToRemove = document.getElementsByTagName("script");
 for (var i=jsToRemove.length; i&gt;=0; i--){
 if (jsToRemove[i] &amp;&amp; jsToRemove[i].getAttribute("src")!=null){
 jsToRemove[i].parentNode.removeChild(jsToRemove[i]);
 }
 }
 var cssToRemove = document.getElementsByTagName("link")
 for (var i=cssToRemove.length; i&gt;=0; i--){
 if (cssToRemove[i] &amp;&amp; cssToRemove[i].getAttribute("href")!=null){
 cssToRemove[i].parentNode.removeChild(cssToRemove[i]);
 }
 }
 }</p>

<p>function import_css(source) {
 var link = document.createElement("link");
 link.href = source;
 link.rel = "stylesheet";
 link.type = "text/css";
 document.getElementsByTagName("head")[0].appendChild(link);
}</p>

<p>//removes memes that won't be wiped by the fix_markup function
function strip_inline_memes(){
 var tagline = document.getElementById('tagline');
 tagline.parentNode.removeChild(tagline);</p>

<p>var footer = document.getElementById('footer');
 footer.parentNode.removeChild(footer);</p>

<p>var images = document.getElementsByTagName("img")
 for (var i=images.length; i&gt;=0; i--){
 if (images[i]){
 images[i].parentNode.removeChild(images[i]);
 }
 }</p>

<p>var memesbyclass = getElementsByClassName('preH1').concat(getElementsByClassName('postH1'))
 for (var i=memesbyclass.length; i&gt;=0; i--){
 if (memesbyclass[i]){
 memesbyclass[i].parentNode.removeChild(memesbyclass[i]);
 }
 }</p>

<p>}
function fix_markup(){
 var header = document.getElementById('header');
 header.parentNode.removeChild(header);</p>

<p>var head1 = getElementsByClassName('h1')
 for (var i=head1.length; i&gt;=0; i--){
 if (head1[i]){
 var newheader = document.createElement("div");
 newheader.innerHTML = "&lt;h1&gt;CS16 / "+ head1[i].innerHTML +"&lt;/h1&gt;";
 head1[i].parentNode.replaceChild(newheader,head1[i]);
 }
 }</p>

<p>var nav = document.getElementById('nav');
 var navcontent = nav.innerHTML;
 var newnav = document.createElement("div");
 newnav.id = "navigation";
 newnav.innerHTML = navcontent;
 nav.parentNode.removeChild(nav);</p>

<p>var content = document.getElementById('content');
 content.insertBefore(newnav, content.firstChild.nextSibling.nextSibling.nextSibling.nextSibling);</p>

<p>}</p>

<p>strip_css_js();
strip_inline_memes();
fix_markup();
import_css("http://zethrae.us/work/bangCS16chan/style.css");
})();
</pre>
This was all intended to be tongue in cheek. But yes, memes from &#8217;06 are rather annoying to see every day. And there wasn&#8217;t even longcat.</p>
]]></content:encoded>
			<wfw:commentRss>http://zethrae.us/blog/2010/playing-with-user-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

