diff booket.js @ 6:e9ad4c625b7a

Add bookmarklet for bookmarking Add a bookmarklet which gathers the URL and title of the current page which can then be copied and pasted into Booket
author Guido Berhoerster <guido+booket@berhoerster.name>
date Wed, 10 Sep 2014 19:45:23 +0200
parents 39c95b9826d2
children a1a934adff8d
line wrap: on
line diff
--- a/booket.js	Tue Sep 09 20:23:58 2014 +0200
+++ b/booket.js	Wed Sep 10 19:45:23 2014 +0200
@@ -24,6 +24,18 @@
 (function () {
 'use strict';
 
+var BOOKMARKLET_URI =
+    'javascript:(function () {' +
+        '\'use strict\';' +
+    '' +
+        'window.alert(\'Copy the following data and paste it into \' +' +
+            '\'Booket:\\n\\n\' + JSON.stringify({' +
+                '\'url\': document.URL,' +
+                '\'title\': document.title' +
+            '}));' +
+    '}) ();';
+
+
 /*
  * utility stuff
  */
@@ -831,6 +843,7 @@
     editorFormElement.querySelector('legend').textContent = 'Add Bookmark';
     editorFormElement.querySelector('input:not([type="hidden"])').accessKey =
         'a';
+    editorFormElement.addEventListener('input', this);
     editorFormElement.addEventListener('click', this);
     editorFormElement.addEventListener('submit', this);
     editorFormElement.addEventListener('reset', this);
@@ -841,6 +854,8 @@
 
     saveFormElement.parentNode.insertBefore(newNode,
         saveFormElement.nextSibling);
+
+    document.querySelector('a#bookmarklet-link').href = BOOKMARKLET_URI;
 };
 
 extend(ActionsView, ObservableMixin);
@@ -855,10 +870,31 @@
 };
 
 ActionsView.prototype.handleEvent = function (e) {
+    var bookmarkletData;
+    var parsedData;
     var tags = [];
     var i;
 
     switch (e.type) {
+    case 'input':
+        if (e.target.name === 'bookmarklet-import') {
+            // get rid of any preceding text
+            bookmarkletData = e.target.value.replace(/^[^{]*/, '');
+
+            try {
+                parsedData = JSON.parse(bookmarkletData);
+            } catch (exception) {
+                return;
+            }
+
+            if (isString(parsedData.url) && parsedData.url !== '') {
+                e.target.form.elements.url.value = parsedData.url;
+            }
+            if (isString(parsedData.title) && parsedData.title !== '') {
+                e.target.form.elements.title.value = parsedData.title;
+            }
+        }
+        break;
     case 'click':
         if (e.target.name === 'more-tags') {
             e.preventDefault();