summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrever Fischer <tdfischer@fedoraproject.org>2012-03-23 13:20:13 -0400
committerTrever Fischer <tdfischer@fedoraproject.org>2012-03-23 13:20:13 -0400
commita4870960e5b906da9a52b58064501c405a217f16 (patch)
treee937afd189ab2f63fb99c490d15cdc6627e68b7c
parentb6f08612e1468e0aaffa37cac74b7c57009fd3f2 (diff)
Save a thumbnail with every page load
-rw-r--r--chrome/zeitgeist.js24
-rw-r--r--npapi-plugin/np-zeitgeist.cc67
2 files changed, 87 insertions, 4 deletions
diff --git a/chrome/zeitgeist.js b/chrome/zeitgeist.js
index 4882dcc..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"});
@@ -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;
@@ -86,6 +91,17 @@ else plugin.setActor("application://chromium-browser.desktop");
chrome.extension.onRequest.addListener (onExtensionRequest);
//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);
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");