From 7f7ace3a8f962dfb47a0137e346c4b6f639c8cea Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Tue, 15 Jul 2014 17:09:05 -0400 Subject: Add webpage's favicon to the request body Note that this is not perfect on Chrome since the favicon is loaded asynchronously after the page loaded event. --- README.rst | 26 ++++++++++++++------------ chrome-extension/bg.js | 13 +++++++------ firefox-addon/lib/main.js | 19 ++++++++++++------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/README.rst b/README.rst index e87ad9b..150cd4e 100644 --- a/README.rst +++ b/README.rst @@ -30,15 +30,17 @@ one the following events occur: The body of the POST request is a JSON object containing the following keys: -========= =========== -Key Name Description -========= =========== -``title`` Title of the webpage (typically the content of the ```` tag) -``url`` URL of the webpage -``time`` Time at which the event occurred. Number of milliseconds elapsed - since 1 January 1970 00:00:00 UTC -``key`` A key identifying the browser sending the request -========= =========== +=========== =========== +Key Name Description +=========== =========== +``title`` Title of the webpage (typically the content of the ``<title>`` tag) +``url`` URL of the webpage +``time`` Time at which the event occurred. Number of milliseconds elapsed + since 1 January 1970 00:00:00 UTC +``key`` A key identifying the browser sending the request +``favicon`` The URL of the webpage's favicon (or ``null`` when there is no + favicon) +=========== =========== For window deactivation events, all the keys except for the ``time`` key are set to ``null``. @@ -48,10 +50,10 @@ Example of request content: .. code:: json {"url":"https://github.com/Thibauth/browsing-activity-tracker", - "time":1405354714678, + "time":1405458411242, "title":"Thibauth/browsing-activity-tracker", - "key":"firefox"} - + "key":"firefox", + "favicon":"https://assets-cdn.github.com/favicon.ico"} Also note that the window activation and deactivation events are triggered by your window manager and vary depending on your configuration. For example, in diff --git a/chrome-extension/bg.js b/chrome-extension/bg.js index 208b7bd..2157d58 100644 --- a/chrome-extension/bg.js +++ b/chrome-extension/bg.js @@ -8,10 +8,11 @@ chrome.storage.onChanged.addListener(function(changes) { } }); -function log(url, title){ +function log(url, title, favicon){ var data = JSON.stringify({ url: url, time: Date.now(), - title: title, key: prefs.key + title: title, key: prefs.key, + favicon: favicon }); var xhr = new XMLHttpRequest(); xhr.open("POST", prefs.callback); @@ -23,7 +24,7 @@ chrome.tabs.onActivated.addListener(function (activeInfo) { if (tab.status === "complete" && tab.active) { chrome.windows.get(tab.windowId, {populate: false}, function(window) { if (window.focused) { - log(tab.url, tab.title); + log(tab.url, tab.title, tab.favIconUrl || null); } }); } @@ -34,7 +35,7 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if (changeInfo.status === "complete" && tab.active) { chrome.windows.get(tab.windowId, {populate: false}, function(window) { if (window.focused) { - log(tab.url, tab.title); + log(tab.url, tab.title, tab.favIconUrl || null); } }); } @@ -42,13 +43,13 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { chrome.windows.onFocusChanged.addListener(function (windowId) { if (windowId == chrome.windows.WINDOW_ID_NONE) { - log(null, null); + log(null, null, null); } else { chrome.windows.get(windowId, {populate: true}, function(window) { if (window.focused) { chrome.tabs.query({active: true, windowId: windowId}, function (tabs) { if (tabs[0].status === "complete") { - log(tabs[0].url, tabs[0].title); + log(tabs[0].url, tabs[0].title, tabs[0].favIconUrl || null); } }); } diff --git a/firefox-addon/lib/main.js b/firefox-addon/lib/main.js index 285397b..f0926c4 100644 --- a/firefox-addon/lib/main.js +++ b/firefox-addon/lib/main.js @@ -1,15 +1,17 @@ var windows = require("sdk/windows").browserWindows; var tabs = require("sdk/tabs"); -var Request = require("sdk/request").Request; -var prefs = require("sdk/simple-prefs").prefs; -var XMLHttpRequest = require("sdk/net/xhr").XMLHttpRequest; +var { Request } = require("sdk/request"); +var { prefs } = require("sdk/simple-prefs"); +var { XMLHttpRequest } = require("sdk/net/xhr"); +var { getFavicon } = require("sdk/places/favicon"); var previous_window = null; -function log(url, title){ +function log(url, title, favicon){ var data = JSON.stringify({ url: url, time: Date.now(), - title: title, key: prefs.key + title: title, key: prefs.key, + favicon: favicon }); var xhr = new XMLHttpRequest(); xhr.open("POST", prefs.callback); @@ -18,13 +20,16 @@ function log(url, title){ function logTab(tab) { if (tab.id === tabs.activeTab.id) { - log(tab.url, tab.title); + getFavicon(tab, function(favicon) { + log(tab.url, tab.title, favicon); + }); } } tabs.on("activate", function () { logTab(tabs.activeTab) }); tabs.on("pageshow", logTab ); + windows.on("activate", function (window) { if (previous_window != window) { previous_window = window; @@ -33,4 +38,4 @@ windows.on("activate", function (window) { logTab(tabs.activeTab) ; }); -windows.on("deactivate", function (window) { log(null, null) }); +windows.on("deactivate", function (window) { log(null, null, null) }); -- cgit v1.2.3-70-g09d2