Permalink
Browse files

remove content script -- now everything runs inside background.html

  • Loading branch information...
1 parent eaf81de commit da61c21792731d93182af4ef20bd761f084feb61 @alexch committed Jun 3, 2011
Showing with 104 additions and 143 deletions.
  1. +12 −3 README.markdown
  2. BIN builds/whence.crx
  3. +2 −92 ext/background.html
  4. +1 −7 ext/manifest.json
  5. +85 −41 ext/whence.js
  6. +4 −0 notes.markdown
View
@@ -42,26 +42,35 @@ We could gather all sorts of other events as well, like tab opens and closes. Bu
# TODO
* Better error if CouchDB is not running/installed
+ * use Chrome Desktop Notifications http://code.google.com/chrome/extensions/notifications.html
* Smoosh successive hits to same host together
* possibly using map/reduce
* Store path as well as host
* Change icon
- * http://raphaeljs.com/icons/
* Visualization
* Replicate/synchronize DBs from multiple computers
* Gather info on which app (other than browser) is active
* Settings page
-* Clone RescueTime
+ * http://code.google.com/chrome/extensions/options.html
* Firefox version
* Safari version
+* Clone RescueTime (Everything version - track active app, not just active tab)
* Include crxmake library
* Include CouchDB executable
* Use IndexedDB for local storage (in case Couch is missing)
* Write server in node.js
* Stick server *inside* CouchDB
* see http://www.livelycouch.org/
* Store tab.favIconUrl (tab is "sender" param of onRequest)
-
+* Limits (e.g. warn if you're on Slashdot too long)
+* Categories a la RescueTime
+* Detect idle time
+ * either stop recording, or record "time since activity" per sample
+* log per-page info e.g. GMail subject and from
+* Geolocation (possibly with GeoCouch)
+ * https://github.com/couchbase/geocouch
+
+
# Credits
- Written by [Alex Chaffee](http://alexch.github.com)
View
Binary file not shown.
View
@@ -1,96 +1,6 @@
<html>
<head>
- <script>
- function say(msg) {
- console.log(msg)
- }
-
- </script>
-
<script src="jquery.js"></script>
-
- <script>
- var urlPrefix = 'http://localhost:5984/';
- var dbName = 'whence';
-
- function couch(method, options) {
- if (options == undefined) {
- options = {};
- }
- var url = urlPrefix + dbName;
- if (options.docId) {
- url += "/" + options
- }
- var data = null;
- if (options.data) {
- data = options.data; // todo: dup
- data.when = new Date();
- }
- $.ajax({
- url: urlPrefix + dbName,
- type: method.toUpperCase(), // type = method
- dataType: 'json', // dataType = response
- data: JSON.stringify(data), // data = request // use https://github.com/douglascrockford/JSON-js/blob/master/json2.js to get JSON in non-Chrome environments
- contentType: "application/json",
- success: function(response) {
- if (options.success == undefined) {
- console.log("Success during " + method + " " + urlPrefix + dbName)
- console.log(response);
- } else {
- console.log("calling callback")
- console.log(options.success)
- options.success(response);
- }
- },
- error: function(e) {
- if (options.error == undefined) {
- console.log("Error during " + method + " " + urlPrefix + dbName)
- console.log(e);
- }
- else {
- options.error(e);
- }
- }
- })
- }
-
- // create the database if necessary
- couch('put', {success: function(response) {
- console.log("Created database " + dbName);
- console.log(response);
- },
- error:function(error) {
- if (error.status == 412) {
- console.log("Found database " + dbName);
- } else {
- say(e.responseText);
- console.log(e);
- }
- }});
-
- // log db info, for fun
- couch('get');
-
- function onRequest(request, sender, callback) {
- if (request.action == 'save') {
- couch('post', {
- data: request.data,
- success: function(response) {
- callback(response);
- },
- error: function(response) {
- console.log("Error");
- console.log(response);
- callback();
- // how to set error? jam it into chrome.extension.lastError?
- }
- });
- }
- };
-
- // Wire up the listener.
- chrome.extension.onRequest.addListener(onRequest);
-
- </script>
+ <script src="whence.js"></script>
</head>
-</html>
+</html>
View
@@ -5,15 +5,9 @@
"icons": { "48": "icon48.png",
"128": "icon128.png" },
"background_page" : "background.html",
- "content_scripts": [{
- "run_at": "document_start",
- "matches": ["http://*/*", "https://*/*"],
- "js": ["jquery.js", "couch.js", "whence.js"]
- }],
"permissions": [
"tabs",
"http://localhost:*/*",
- "http://localhost/*",
- "http://*/*"
+ "http://localhost/*"
]
}
View
@@ -1,45 +1,89 @@
+var urlPrefix = 'http://localhost:5984/';
+var dbName = 'whence';
-var whence = {
- ticker: null,
-
- host: window.location.host.replace('www.',''),
-
- start: function() {
- var self = this;
- if (this.ticker == null) {
- console.log("starting tracking " + this.host);
- function tick() {
- var blob = {host: self.host};
-
- chrome.extension.sendRequest({'action' : 'save', data: blob}, function(response) {
- // console.log(response);
- if (response == undefined) {
- // error
- console.log("Error on " + self.host + " saving " + blob);
- console.log("lastError=")
- console.log(chrome.extension.lastError) // ??
- } else {
- // ok
- }
- });
- }
- this.ticker = setInterval(tick, 1000);
- }
- },
-
- stop: function() {
- console.log("stopping tracking " + this.host);
- if (this.ticker != null) {
- clearInterval(this.ticker);
- this.ticker = null;
+function couch(method, options) {
+ if (options == undefined) {
+ options = {};
+ }
+ var url = urlPrefix + dbName;
+ if (options.docId) {
+ url += "/" + options.docId;
+ }
+ var data = null;
+ if (options.data) {
+ data = options.data; // todo: dup
+ data.when = new Date();
+ }
+ $.ajax({
+ url: urlPrefix + dbName,
+ type: method.toUpperCase(), // type = method
+ dataType: 'json', // dataType = response
+ data: JSON.stringify(data), // data = request // use https://github.com/douglascrockford/JSON-js/blob/master/json2.js to get JSON in non-Chrome environments
+ contentType: "application/json",
+ success: function(response) {
+ if (options.success == undefined) {
+ console.log("Success during " + method + " " + urlPrefix + dbName)
+ console.log(response);
+ } else {
+ options.success(response);
+ }
+ },
+ error: function(e) {
+ if (options.error == undefined) {
+ console.log("Error during " + method + " " + urlPrefix + dbName)
+ console.log(e);
+ }
+ else {
+ options.error(e);
+ }
}
+ })
+}
+
+// create the database if necessary
+couch('put', {success: function(response) {
+ console.log("Created database " + dbName);
+ console.log(response);
+},
+error:function(error) {
+ if (error.status == 412) {
+ console.log("Found database " + dbName);
+ } else {
+ say(e.responseText);
+ console.log(e);
}
-};
+}});
+
+// log db info, for fun
+couch('get');
-whence.start();
-$(window).blur(function(){
- whence.stop();
-});
-$(window).focus(function(){
- whence.start();
-});
+function parseUrl(url) {
+ var parts = url.match(/(.*):\/\/([^\/]*)(\/.*)/);
+ if (parts == null) {
+ console.log("Couldn't parse url '" + url + "'")
+ return {host: '?'};
+ }
+ return {
+ protocol: parts[1],
+ host: parts[2],
+ path: parts[3]
+ }
+}
+
+setInterval(function() {
+ chrome.tabs.getSelected(null, function(tab) {
+ if (tab != undefined) { // we have a selected tab
+ chrome.windows.get(tab.windowId, function(window) {
+ if (window.focused) { // we haven't lost focus
+ host = parseUrl(tab.url).host;
+ couch('post', {
+ data: {host: host},
+ success: function() {
+ // silence is golden
+ }
+ });
+ }
+ });
+ }
+ });
+}, 1000)
View
@@ -1,5 +1,7 @@
# Developer notes
+looks like about 4K per record (minimum), and compacting removes all old versions
+
"CouchDB is specifically intended to run in the client, and to provide a form of location transparency where your entire application can be running locally or remotely and you really don't know the difference except that perhaps some of your data is stale until you reconnect to the server."
Unfortunately you can't connect to it via XHR (JS in a browser) so we need an intermediate Ruby server to add the "Access-Control-Allow-Origin" header. See bug https://issues.apache.org/jira/browse/COUCHDB-431
@@ -19,3 +21,5 @@ http://www.sowbug.org/mt/2008/06/couchdb-cheat-sheet.html
http://blog.fupps.com/2010/04/20/the-antepenultimate-couchdb-reference-card/
http://www.andyjarrett.co.uk/blog/index.cfm/2010/3/29/CouchDB-quick-refstarting-guide
+Chrome Extensions
+http://code.google.com/chrome/extensions/extension.html

0 comments on commit da61c21

Please sign in to comment.