User:Eleanorg/Thematic1.1/traceroute plugin v1: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "==contentscript.js== Currently returning Chrome error: Uncaught SyntaxError: Unexpected token , What's the problem with the comma? <source lang=javascript> // ascertains curren...")
 
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
Get the plugin here:
http://pzwart3.wdka.hro.nl/~egreenhalgh/traceroutePlugin.crx
==contentscript.js==
==contentscript.js==


Currently returning Chrome error: Uncaught SyntaxError: Unexpected token ,
Pages initially load at fully opacity, and fade out to their appropriate level when the traceroute has completed.
What's the problem with the comma?
 
<source lang=javascript>
<source lang=javascript>
// make xhr request to traceroute counting script; return hop count converted to a float
// thanks to Jonas for ajax syntactic sugar
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();
};
 


// ascertains current page url
// begin to fade out the page
var currentUrl = window.location.href;
waitUntilExists(document,function(){ 
  $("body").fadeTo(9000, 0.7);
});


// sends the current page url to script, and gets back number of hops as an opacity value
// when xhr request returns data, stop page fade and set to appropriate opacity
$.ajax({
function opacityFade(data) {
  type: "POST",
   if(data) {
  url : "http://pzwart3.wdka.hro.nl/~egreenhalgh/hopsToPercent2.py",
  $("body").stop().fadeTo(2000, data);
  data : {url : currentUrl},
  //console.log(data);
   success: function(data) {
    $(body).css({"opacity", data});
   }
   }
});
}
 
getTraceRouteCount(opacityFade);
</source>
</source>


Line 30: Line 56:
     "default_icon": "icon.png"
     "default_icon": "icon.png"
   },
   },
   "permissions": ["history", "http://pzwart3.wdka.hro.nl/~egreenhalgh/hopsToPercent2.py"],
   "permissions": ["http://*/"],
   "content_scripts": [
   "content_scripts": [
     {
     {
       "matches": ["*://*/*"],
       "matches": ["*://*/*"],
       "js": ["jquery.js", "contentscript2.js"],
       "js": ["jquery.js", "waituntilexists.js", "contentscript4.js"],
       "run_at": "document_end"
       "run_at": "document_end"
     }
     }
Line 48: Line 74:
#!/usr/bin/python
#!/usr/bin/python
#-*- coding:utf-8 -*-
#-*- coding:utf-8 -*-
print "Content-type: text/html\n"


import os # lets you use system commands inside the python script
import os, cgi, cgitb
cgitb.enable()


###---------- GET URL FROM EXTENSION---------
###---------- GET NO. OF HOPS--------------


form = cgi.FieldStorage()
form = cgi.FieldStorage()
site = form["url"].value # url grabbed from the plugin's javascript
url = form["url"].value # dynamic url grabbed from the plugin's javascript
 
command = "traceroute " + url + " | wc -l"  # concatenates your site variable inside command to be sent to system
###---------- 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'
traceroute = os.popen(command,'r')            # popen function opens system command with argument 'r' for 'read'
hopcount = float(traceroute.read()) - 1
hopcount = float(traceroute.read()) - 2




Line 67: Line 91:


def makePercent():
def makePercent():
   percent = hopcount / 40                
   percent = 1 - (hopcount / 31)                
   return(percent)
   return(percent)


print makePercent()
</source>
</source>

Latest revision as of 10:11, 15 December 2011

Get the plugin here: http://pzwart3.wdka.hro.nl/~egreenhalgh/traceroutePlugin.crx

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
// thanks to Jonas for ajax syntactic sugar
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.

#!/usr/bin/python
#-*- coding:utf-8 -*-
print "Content-type: text/html\n"

import os, cgi, cgitb					
cgitb.enable()

###---------- GET NO. OF HOPS--------------

form = cgi.FieldStorage()
url = form["url"].value				# dynamic url grabbed from the plugin's javascript
command = "traceroute " + url + " | 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()) - 2


###--------- CONVERT TO %------------------

def makePercent():
  percent = 1 - (hopcount / 31)               
  return(percent)

print makePercent()