summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeif Lotfy <seif@lotfy.com>2012-03-23 20:23:24 +0100
committerSeif Lotfy <seif@lotfy.com>2012-03-23 20:23:24 +0100
commit8d73589f4e1d3f07ea337bac42bfad2514a8c020 (patch)
tree49893a0a53dac1c5ed8d6fa967ac98e40ba18de2
parent12b8113c433846e88e82f94658d17c18bacc6f57 (diff)
parenta4870960e5b906da9a52b58064501c405a217f16 (diff)
merge
-rw-r--r--chrome/zeitgeist.js48
-rw-r--r--npapi-plugin/np-zeitgeist.cc67
2 files changed, 96 insertions, 19 deletions
diff --git a/chrome/zeitgeist.js b/chrome/zeitgeist.js
index 534d3d7..82bcb01 100644
--- a/chrome/zeitgeist.js
+++ b/chrome/zeitgeist.js
@@ -1,6 +1,7 @@
var plugin = document.embeds[0];
var tabInfo = {};
var tabIdTimeouts = {};
+var currentTabs = {};
function onTabCreated (tab) {
chrome.tabs.executeScript(tab.id, {file: "content_script.js"});
@@ -13,9 +14,10 @@ function onTabRemoved (tabid) {
function onTabUpdated (tabid, changeInfo, tab) {
if (!changeInfo.url) return;
window.clearTimeout(tabIdTimeouts[tabid])
- tabIdTimeouts[tabid] = window.setTimeout(function(){
- chrome.tabs.executeScript(tabid, {file: "content_script.js"});},
- 5000);
+ tabIdTimeouts[tabid] = window.setTimeout(function(){
+ console.log("sending event for " + tab.url);
+ chrome.tabs.executeScript(tabid, {file: "content_script.js"});},
+ 5000);
}
function onBookmarkCreated (bookmarkid, bookmark) {
@@ -27,7 +29,6 @@ function onBookmarkCreated (bookmarkid, bookmark) {
}
function sendAccessEvent (documentInfo, tabid) {
- console.log("Sending access event for " + documentInfo.url);
var url = documentInfo.url;
var origin = documentInfo.origin;
var domain = documentInfo.domain;
@@ -39,10 +40,14 @@ function sendAccessEvent (documentInfo, tabid) {
}
var mimetype = documentInfo.mimeType;
var title = documentInfo.title;
- plugin.insertEvent(url,
- domain,
- mimetype ? mimetype : "text/html",
- title);
+ plugin.insertEvent(url,
+ domain,
+ mimetype ? mimetype : "text/html",
+ title);
+ console.log("save thumbnail for "+currentTabs[tabid]+": "+url);
+ chrome.tabs.captureVisibleTab(currentTabs[tabid], {format:"jpeg", quality:5}, function(dataUrl) {
+ plugin.saveSnapshot(url, dataUrl);
+ });
documentInfo.sentAccess = true;
tabInfo[tabid] = documentInfo;
@@ -51,7 +56,6 @@ function sendAccessEvent (documentInfo, tabid) {
function sendLeaveEvent (tabid) {
var documentInfo = tabInfo[tabid];
if (documentInfo == null || documentInfo.sentAccess != true) return;
- console.log("Sending leave event for " + documentInfo.url);
var url = documentInfo.url;
var origin = documentInfo.origin;
@@ -76,13 +80,8 @@ function sendLeaveEvent (tabid) {
// this works in chrome 7,8,9
function onExtensionRequest (request, sender, sendResponse) {
var id = sender.tab.id;
- if (tabInfo[id] == undefined || tabInfo[id].url != request.url) {
- /*if (tabInfo[id] != undefined) {
- console.log("Leaving " + tabInfo[id].url);
- sendLeaveEvent(id);
- }*/
- sendAccessEvent(request, id);
- }
+ sendLeaveEvent(id);
+ sendAccessEvent(request, id);
}
var is_chromium = /chromium/.test( navigator.userAgent.toLowerCase() );
@@ -90,16 +89,27 @@ if (!is_chromium) plugin.setActor("application://google-chrome.desktop");
else plugin.setActor("application://chromium-browser.desktop");
chrome.extension.onRequest.addListener (onExtensionRequest);
-chrome.bookmarks.onCreated.addListener (onBookmarkCreated);
+//chrome.bookmarks.onCreated.addListener (onBookmarkCreated);
chrome.tabs.onUpdated.addListener (onTabUpdated);
+chrome.tabs.onCreated.addListener(
+ function (tab) {
+ currentTabs[tab.id] = tab.windowId;
+ }
+);
+
+chrome.tabs.onRemoved.addListener(
+ function (tabid) {
+ delete currentTabs[tabid];
+ }
+);
//chrome.tabs.onCreated.addListener (onTabCreated);
-chrome.tabs.onRemoved.addListener (onTabRemoved);
+//chrome.tabs.onRemoved.addListener (onTabRemoved);
chrome.windows.getAll({"populate" : true}, function (windows) {
for (var i = 0; i < windows.length; i++) {
var tabs = windows[i].tabs;
for (var j = 0; j < tabs.length; j++) {
- chrome.tabs.executeScript(tabs[j].id, {file: "content_script.js"});
+ chrome.tabs.executeScript(tabs[j].id, {file: "content_script.js"});
}
}
});
diff --git a/npapi-plugin/np-zeitgeist.cc b/npapi-plugin/np-zeitgeist.cc
index 6c2c04a..0066ab0 100644
--- a/npapi-plugin/np-zeitgeist.cc
+++ b/npapi-plugin/np-zeitgeist.cc
@@ -51,6 +51,7 @@ hasMethod(NPObject* obj, NPIdentifier methodName) {
if (!strcmp(name, "insertEvent")) return true;
else if (!strcmp(name, "setActor")) return true;
+ else if (!strcmp(name, "saveSnapshot")) return true;
return false;
}
@@ -59,6 +60,7 @@ static bool
invokeInsertEvent (NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
/* args should be: url, origin, mimetype, title, [interpretation] */
+ g_debug("Inserting event");
char *url, *origin, *mimetype, *title;
char *interpretation = NULL;
const char *manifestation = NULL;
@@ -66,9 +68,11 @@ invokeInsertEvent (NPObject *obj, const NPVariant *args, uint32_t argCount, NPVa
const NPString *np_s;
ZeitgeistEvent *event;
+ g_debug("arg count: %d", argCount);
if(argCount < 4 || argCount > 6)
{
npnfuncs->setexception(obj, "exception during invocation");
+ g_debug("too many or too few args");
return false;
}
@@ -145,6 +149,64 @@ invokeInsertEvent (NPObject *obj, const NPVariant *args, uint32_t argCount, NPVa
}
static bool
+invokeSaveSnapshot (NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
+{
+ char *url;
+ char *screenshotURL = NULL;
+ const NPString *np_s;
+ np_s = &NPVARIANT_TO_STRING (args[0]);
+ url = g_strndup(np_s->UTF8Characters, np_s->UTF8Length);
+ np_s = &NPVARIANT_TO_STRING (args[1]);
+ screenshotURL = g_strndup(np_s->UTF8Characters, np_s->UTF8Length);
+
+ if (!screenshotURL)
+ return false;
+
+ gsize len = strlen(screenshotURL) - 22;
+ char *img = new char[len];
+ memset(img, 0, len);
+ memcpy(img, screenshotURL + 22, len);
+
+ // update thumbnail
+ gchar *thumbnail_path;
+ gchar *thumbnail_filename = NULL;
+ gchar *thumbnail_dir;
+ gchar *csum;
+
+ // create dir if it doesn't exist
+ thumbnail_dir = g_build_filename (g_get_home_dir (),
+ ".thumbnails",
+ "large",
+ NULL);
+ if (!g_file_test(thumbnail_dir, G_FILE_TEST_IS_DIR)) {
+ g_mkdir_with_parents (thumbnail_dir, 0755);
+ }
+ g_free (thumbnail_dir);
+
+ csum = g_compute_checksum_for_string (G_CHECKSUM_MD5, url, -1);
+
+ thumbnail_filename = g_strconcat (csum, ".png", NULL);
+ thumbnail_path = g_build_filename (g_get_home_dir (),
+ ".thumbnails",
+ "large",
+ thumbnail_filename,
+ NULL);
+ g_free (csum);
+
+ guchar *jpg_data = g_base64_decode(img, &len);
+
+ g_debug("Writing thumbnail to %s", thumbnail_path);
+ g_file_set_contents(thumbnail_path, (gchar*)jpg_data, len, NULL);
+
+ g_free (img);
+ g_free (jpg_data);
+ g_free (thumbnail_filename);
+ g_free (thumbnail_path);
+
+ return true;
+}
+
+static bool
invokeSetActor (NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
const NPString *np_s;
@@ -175,6 +237,7 @@ invoke(NPObject* obj, NPIdentifier methodName, const NPVariant *args, uint32_t a
name = npnfuncs->utf8fromidentifier(methodName);
+ g_debug("Calling %s", name);
if(name)
{
if (!strcmp (name, "insertEvent"))
@@ -185,6 +248,10 @@ invoke(NPObject* obj, NPIdentifier methodName, const NPVariant *args, uint32_t a
{
return invokeSetActor(obj, args, argCount, result);
}
+ else if (!strcmp (name, "saveSnapshot"))
+ {
+ return invokeSaveSnapshot(obj, args, argCount, result);
+ }
}
npnfuncs->setexception(obj, "exception during invocation");