User:Eleanorg/Thematic1.1/traceroute plugin v1
contentscript.js
Pages initially load at fully opacity, and fade out to their appropriate level when the traceroute has completed.
// make xhr request to traceroute counting script; return hop count converted to a float
function getTraceRouteCount(callback) {
var currentUrl = window.location.host;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = xhr.responseText;
callback(data);
} else {
callback(null);
}
}
}
var url = "http://pzwart3.wdka.hro.nl/~egreenhalgh/cgi-bin/hopsToPercent2.py?url="+ currentUrl;
xhr.open('GET', url, true);
xhr.send();
};
// begin to fade out the page
waitUntilExists(document,function(){
$("body").fadeTo(9000, 0.7);
});
// when xhr request returns data, stop page fade and set to appropriate opacity
function opacityFade(data) {
if(data) {
$("body").stop().fadeTo(2000, data);
//console.log(data);
}
}
getTraceRouteCount(opacityFade);
manifest.json
Gives extension permission to use python script on PZI server.
{
"name": "traceroute1",
"version": "1.0",
"description": "changes the opacity of web pages",
"browser_action": {
"default_icon": "icon.png"
},
"permissions": ["http://*/"],
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["jquery.js", "waituntilexists.js", "contentscript4.js"],
"run_at": "document_end"
}
]
}
hopsToPercent2.py
Lives on the PZI server in my public_html.
#!/usr/bin/python
#-*- coding:utf-8 -*-
import os # lets you use system commands inside the python script
###---------- GET URL FROM EXTENSION---------
form = cgi.FieldStorage()
site = form["url"].value # url grabbed from the plugin's javascript
###---------- GET NO. OF HOPS--------------
command = "traceroute " + site + " | wc -l" # concatenates your site variable inside command to be sent to system
traceroute = os.popen(command,'r') # popen function opens system command with argument 'r' for 'read'
hopcount = float(traceroute.read()) - 1
###--------- CONVERT TO %------------------
def makePercent():
percent = hopcount / 40
return(percent)