Permalink
Browse files

add webRequest tracking (+30x)

  • Loading branch information...
1 parent f772221 commit de202e6f4669df8ecb746d5a162aed37aded5b9a @presbrey presbrey committed Oct 17, 2012
Showing with 182 additions and 112 deletions.
  1. +2 −0 .gitignore
  2. +9 −0 Makefile
  3. +121 −0 background.coffee
  4. +0 −88 background.js
  5. +5 −0 manifest.json
  6. +8 −0 popup.html
  7. +1 −0 popup.js
  8. +36 −0 skin.coffee
  9. +0 −24 skin.js
View
@@ -0,0 +1,2 @@
+background.js
+skin.js
View
@@ -0,0 +1,9 @@
+
+SRC=$(wildcard *.coffee)
+LIB=$(SRC:%.coffee=%.js)
+
+.PHONY: coffee
+coffee: $(LIB)
+
+%.js: %.coffee
+ coffee -bp $< > $@
View
@@ -0,0 +1,121 @@
+# background.js
+
+onBeforeSendHeaders = do (types=['application/rdf+xml','text/n3','text/turtle']) ->
+ (d) ->
+ setup = false
+ for elt in d.requestHeaders when elt.name.toLowerCase() is 'accept'
+ setup = true
+ values = elt.value.split ','
+ for j, i in values
+ if j.indexOf(';') < 0
+ values[i] = values[i] + ';q=0.9'
+ for j, i in types
+ values.unshift j
+ elt.value = values.join ','
+ break
+ if not setup and d.requestHeaders.push
+ values = []
+ for j in types
+ values.unshift j
+ d.requestHeaders.push({name: 'Accept', value: values.join ','})
+ return {requestHeaders: d.requestHeaders}
+
+requests = {}
+
+init = (d) ->
+ requests[d.requestId] = true
+ return { cancel: true }
+
+skinnable = (d) ->
+ return requests[d.requestId] == true
+
+skin = (d) ->
+ delete requests[d.requestId]
+ chrome.tabs.update(d.tabId, {
+ url: chrome.extension.getURL 'skin.html?uri='+encodeURIComponent(d.url),
+ })
+
+onHeadersReceived = (d) ->
+ for header in d.responseHeaders
+ if header.name.match(/content-type/i) and header.value.match(/\/(n3|rdf|turtle)/)
+ return init d
+
+onErrorOccurred = (d) ->
+ if skinnable d
+ skin d
+
+events = {
+ 'onBeforeSendHeaders': {
+ callback: onBeforeSendHeaders,
+ filter: {types: ["main_frame"], urls: ["<all_urls>"]},
+ extras: ['requestHeaders', 'blocking'],
+ },
+ 'onHeadersReceived': {
+ callback: onHeadersReceived,
+ filter: {types: ["main_frame"], urls: ["<all_urls>"]},
+ extras: ['responseHeaders', 'blocking'],
+ },
+ 'onErrorOccurred': {
+ callback: onErrorOccurred,
+ filter: {types: ["main_frame"], urls: ["<all_urls>"]},
+ },
+}
+
+install = (api=chrome.webRequest) ->
+ for event, meta of events
+ console.log 'setup/addListener: ' + event
+ if meta.extras
+ api[event].addListener meta.callback, meta.filter, meta.extras
+ else
+ api[event].addListener meta.callback, meta.filter
+ console.log 'setup: success!'
+
+class Client
+ constructor: (@port) ->
+ @hooks = ({api, handler: handler.bind(@), extras} for {api, handler, extras} in Client.hooks)
+ @id = port.sender.tab.id
+ for {api, handler, extras} in @hooks
+ if extras
+ api.addListener handler, {urls: ['<all_urls>'], tabId: @id}, extras
+ else
+ api.addListener handler, {urls: ['<all_urls>'], tabId: @id}
+ disconnect: ->
+ for {api, handler} in @hooks
+ api.removeListener handler
+ onCompleted: (d) ->
+ @port.postMessage d
+ onHeadersReceived: (d) ->
+ @port.postMessage d
+ onErrorOccurred: (d) ->
+ @port.postMessage d
+ @hooks: [
+ api: chrome.webRequest.onCompleted
+ handler: @::onCompleted
+ extras: ['responseHeaders']
+ ,
+ api: chrome.webRequest.onHeadersReceived
+ handler: @::onHeadersReceived
+ extras: ['responseHeaders']
+ ,
+ api: chrome.webRequest.onErrorOccurred
+ handler: @::onErrorOccurred
+ ]
+
+class Server
+ constructor: (@clientClass=Client) ->
+ @clients = {}
+ chrome.extension.onConnect.addListener @onConnect.bind @
+
+ onConnect: (port) ->
+ id = port.portId_
+ port.onDisconnect.addListener @onDisconnect.bind @
+ @clients[id] = new @clientClass port
+
+ onDisconnect: (port) ->
+ id = port.portId_
+ if @clients[id]
+ @clients[id].disconnect()
+ delete @clients[id]
+
+server = new Server
+install()
View
@@ -1,88 +0,0 @@
-// background.js
-
-onBeforeSendHeaders = (function factory(types) {
- return function(d) {
- var setup = false;
- for (var i in d.requestHeaders) {
- var name = d.requestHeaders[i].name.toLowerCase();
- if (name == 'accept') {
- setup = true;
- var values = d.requestHeaders[i].value.split(',');
- for (var j in values)
- if (values[j].indexOf(';') < 0)
- values[j] = values[j] + ';q=0.9';
- for (var j in types)
- values.unshift(types[j]);
- d.requestHeaders[i].value = values.join(',');
- break;
- }
- }
- if (!setup && d.requestHeaders.push) {
- var values = [];
- for (var j in types)
- values.unshift(types[j]);
- d.requestHeaders.push({name: 'Accept', value: values.join(',')});
- }
- return {requestHeaders: d.requestHeaders};
- };
-})(['application/rdf+xml','text/n3','text/turtle']);
-
-var requests = {};
-
-function init(d) {
- requests[d.requestId] = true;
- return { cancel: true };
-}
-
-function skinnable(d) {
- return requests[d.requestId] == true;
-}
-
-function skin(d) {
- delete requests[d.requestId];
- chrome.tabs.update(d.tabId, {
- url: chrome.extension.getURL('skin.html?uri='+encodeURIComponent(d.url)),
- });
-}
-
-function onHeadersReceived(d) {
- for (var i in d.responseHeaders) {
- var header = d.responseHeaders[i];
- if (header.name && header.name.match(/content-type/i)
- && header.value.match(/\/(n3|rdf|turtle)/))
- return init(d);
- }
-}
-
-function onErrorOccurred(d) {
- if (skinnable(d))
- skin(d);
-}
-
-var events = {
- 'onBeforeSendHeaders': {
- callback: onBeforeSendHeaders,
- filter: {types: ["main_frame"], urls: ["<all_urls>"]},
- extras: ['requestHeaders', 'blocking'],
- },
- 'onHeadersReceived': {
- callback: onHeadersReceived,
- filter: {types: ["main_frame"], urls: ["<all_urls>"]},
- extras: ['responseHeaders', 'blocking'],
- },
- 'onErrorOccurred': {
- callback: onErrorOccurred,
- filter: {types: ["main_frame"], urls: ["<all_urls>"]},
- },
-};
-
-(function setup(api) {
- for (j in events) {
- console.log('setup/addListener: ' + j);
- if (events[j].extras && api[j])
- api[j].addListener(events[j].callback, events[j].filter, events[j].extras);
- else if (api[j])
- api[j].addListener(events[j].callback, events[j].filter);
- }
- console.log('setup: success!');
-})(chrome.webRequest);
View
@@ -7,6 +7,11 @@
},
"manifest_version": 2,
"name": "Tabulator",
+ "page_action": {
+ "default_icon": "rdf_flyer.48.gif",
+ "default_popup": "popup.html",
+ "default_title": "Linked Data Browser"
+ },
"permissions": [
"file:///*",
"http://*/*",
View
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body>
+<script type="text/javascript" src="popup.js"></script>
+</body>
+</html>
View
@@ -0,0 +1 @@
+document.write('TODO');
View
@@ -0,0 +1,36 @@
+# skin.js
+
+port = null
+connect = ->
+ port = chrome.extension.connect()
+ port.onMessage.addListener (data) ->
+ {method, url, statusLine, responseHeaders} = data
+ #console.log "#{method} <#{url}> #{statusLine}"
+ #for {name, value} in data.responseHeaders when name is 'Location'
+ # console.log "#{name}: #{value}"
+ port.onDisconnect.addListener ->
+ setTimeout(connect, 1000)
+connect()
+
+load = (uri) ->
+ window.document.title = uri
+ chrome.tabs.getCurrent (tab) ->
+ chrome.pageAction.setPopup
+ popup: 'popup.html?uri='+encodeURIComponent(uri)
+ tabId: tab.id
+ chrome.pageAction.show tab.id
+ kb = tabulator.kb
+ subject = kb.sym(uri)
+ tabulator.outline.GotoSubject(subject, true, undefined, true, undefined)
+
+jQuery ->
+ qs = do ->
+ r = {}
+ for elt in window.location.search.substr(1).split('&')
+ p = elt.split '='
+ unless p.length is 2
+ continue
+ r[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "))
+ return r
+ if qs.uri
+ load qs.uri
View
24 skin.js
@@ -1,24 +0,0 @@
-// skin.js
-
-function load(uri) {
- window.document.title = uri;
- var kb = tabulator.kb;
- var subject = kb.sym(uri);
- tabulator.outline.GotoSubject(subject, true, undefined, true, undefined);
-}
-
-jQuery(function() {
- var qs = (function(a) {
- if (a == "") return {};
- var b = {};
- for (var i = 0; i < a.length; ++i) {
- var p=a[i].split('=');
- if (p.length != 2) continue;
- b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
- }
- return b;
- })(window.location.search.substr(1).split('&'));
-
- if (qs.uri)
- load(qs.uri);
-});

0 comments on commit de202e6

Please sign in to comment.