Monday, November 19, 2007

Concise

The Register has been having fun with a script that removes all mention of the word "iPhone" from webpages; a necessary function these days. Better, they developed it to work on an iPhone; but just check out the code.

// JavaScript here

//This one thinks it's an object
var myRequest = new XMLHttpRequest();

//This is the text we're going to change the word "iPhone" to
var changeTo = "";

//This is our home page, and the site that leaving will unload the app
var home = 'http://www.theregister.co.uk';

function startUp() {
changeTo = readCookie("newName")
if (changeTo == null) {
changeTo = window.prompt("So what would better suit the iPhone?");
createCookie("newName", changeTo, 1);
}
loadRegister(home);
}

function loadRegister(targetURL) {

//targetDomain is set to a string containing the site (but not directories or file) that the user clicked on
var targetDomain = targetURL.substring(targetURL.indexOf(".", 8)+1, targetURL.indexOf("/", 8));

//We compare that to our home page
if (home.indexOf(targetDomain) == -1) {
alert("Moving Off Site: " + targetDomain);
//This line unloads this application, as the targetURL replaces this document
parent.parent.location=targetURL;
}

//Then we load the page
myRequest.open("GET", targetURL);
myRequest.onload = targetLoaded;
myRequest.send();
}

function targetLoaded() {
var loadedSite = myRequest.responseText;

loadedSite = loadedSite.replace(/iPhone /g, changeTo + " ");
loadedSite = loadedSite.replace(/ iPhone/g, " " + changeTo);

var counter;

var loadedDocument = parent.frames[0].document;

loadedDocument.open();
loadedDocument.write(loadedSite);
loadedDocument.close();
//This is our horrible bodge which waits 10 seconds for the page to load
setTimeout('pageLoaded()', 10000);
}

function pageLoaded() {
//This loops through every link on the page (241 on the El Reg home page when we were testing this) and adds an "onclick" even listener
for (i=0; i < parent.frames[0].document.links.length; i++) {
parent.frames[0].document.links[i].onclick = linkClicked;
}
}

function linkClicked() {
loadRegister(this.href);
//We return "false" so the browser dosen't attempt to load the link clicked on.
return false;
}

function returnHome() {
loadRegister(home);
}

function changeName() {
eraseCookie("newName");
changeTo = window.prompt("So what would better suit the iPhone?");
createCookie("newName", changeTo, 1);
}

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
//--
Nurgs! My brane! Now this is why I like Python...
#! usr/bin/env/ python

import string
import urllib
import webbrowser

print ('Enter a URL for de-iPhoning')

input = raw_input()
url = urllib.urlopen(input)
data = url.read()
snip = input.replace('http://www.', '')
fname =('/home/yorksranter/.mozilla/firefox/ydirmggn.default/Cache/'+snip)
f = open(fname, 'w')
d = data.replace('iPhone', '')
f.write(d)

webbrowser.open_new_tab(fname)
Obviously you'll want to replace the file path with your own browser cache, unless you like this blog so much you named your user account after it. Windows users should do the same and remove the first line.

No wonder the Reg guy ended up saying this:
We also decided that we're not going to develop anything else for the iPhone until there's a proper development kit, allowing the use of a proper programming language, and some decent documentation too.


All TYR Labs code in this post has passed the rigorous Atwood Certification Test.

No comments:

kostenloser Counter