More Javascript goodness

More Opera Javascript goodness this week. I doctored up my first attempt at a User Javascript, so it now actually looks good. Here's the new version: if ( location.href.match(/^http://www.google.com/search/) ) { document.addEventListener( 'load', function (e) { var anchors = document.getElementsByTagName("a"); for(i=0; i<anchors.length; i++) { if (anchors[i].innerHTML == "Cached") { old_href = anchors[i].href; tnode = document.createTextNode(" - "); anchors[i].parentNode.insertBefore(tnode, anchors[i].nextSibling); linknode = document.createElement("a"); linknode.setAttribute("href", old_href+'&lr=&strip=1'); linktext = document.createTextNode("Cached text only"); linknode.appendChild(linktext); anchors[i].parentNode.insertBefore(linknode, tnode.nextSibling); i = i+2; } } return true; }, false ); }

I also cooked myself up a custom Opera button. It runs a little piece of JavaScript that pops up a window showing the linked elements for the page. It breaks them down into RSS feeds, stylesheets, and lumps the rest together, providing a hyperlink to each element. The original idea was to give me an easy way to access linked RSS feeds. Opera provides a nice icon for accessing these, but it only works with Opera's internal RSS reader, which I don't use.

The line I added for this button is below. You can add it to the [Document Toolbar.content] section of whatever your current toolbar file is in ~/.opera/toolbar. I would have liked to provide the button as a link you could drag to the toolbar, but I wasn't able to get it to work, possibly because of the 974 byte limit. However, you can try it as a Boolmarklet. Anyway, here's the line:
Button6, "Links"="Go to page, "javascript:var els=document.getElementsByTagName('link'); var rsslinks=''; var otherlinks=''; var stylelinks=''; for(i=0;i<els.length;i++){ if(els[i].title){ linktext=els[i].title; }else if(els[i].rel){ linktext=els[i].rel; if(els[i].rel=='stylesheet'){ re=/.*/([^/]+)$/; linktext=els[i].href.replace(re,'$1'); if(linktext==''){linktext=els[i].rel;} } }else{ linktext=els[i].href; } if(els[i].rel == 'alternate'){ rsslinks+='<li><a href="'+els[i].href+"'>'+linktext+'</a></li>'; }else if(els[i].rel == 'stylesheet'){ stylelinks+='<li><a href="'+els[i].href+"'>'+linktext+'</a></li>'; }else{ otherlinks+='<li><a href="'+els[i].href+"'>'+linktext+'</a></li>'; } } if(rsslinks=='' && otherlinks=='' && stylelinks==''){ window.alert('No link elements found.') }else{ pag0101=open('','','width=400,height=400'); pag0101.document.write('<html><head><title>Linked Elements</title><base target=_blank></head><body>'); if(rsslinks!=''){ pag0101.document.write('<h4>RSS Feeds</h4><ul>',rsslinks,'</ul>'); } if(stylelinks!=''){ pag0101.document.write('<h4>Style Sheets</h4><ul>',stylelinks,'</ul>'); } if(otherlinks!=''){ pag0101.document.write('<h4>Other Linked Elements</h4><ul>',otherlinks,'</ul>'); } pag0101.document.write('</body></html>'); }", , "Show link elements", "Show newsfeeds""
Needless to say, you will want to change "Button6" to whatever is appropriate for your setup.

You can reply to this entry by leaving a comment below. This entry accepts Pingbacks from other blogs.

Add your comments #

A comment body is required. No HTML code allowed. URLs starting with http:// or ftp:// will be automatically converted to hyperlinks.