Mercurial > projects > booket
comparison 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 |
comparison
equal
deleted
inserted
replaced
5:39c95b9826d2 | 6:e9ad4c625b7a |
---|---|
22 */ | 22 */ |
23 | 23 |
24 (function () { | 24 (function () { |
25 'use strict'; | 25 'use strict'; |
26 | 26 |
27 var BOOKMARKLET_URI = | |
28 'javascript:(function () {' + | |
29 '\'use strict\';' + | |
30 '' + | |
31 'window.alert(\'Copy the following data and paste it into \' +' + | |
32 '\'Booket:\\n\\n\' + JSON.stringify({' + | |
33 '\'url\': document.URL,' + | |
34 '\'title\': document.title' + | |
35 '}));' + | |
36 '}) ();'; | |
37 | |
38 | |
27 /* | 39 /* |
28 * utility stuff | 40 * utility stuff |
29 */ | 41 */ |
30 | 42 |
31 function isNumber(number) { | 43 function isNumber(number) { |
829 | 841 |
830 editorFormElement = newNode.querySelector('form.bookmark-editor-form'); | 842 editorFormElement = newNode.querySelector('form.bookmark-editor-form'); |
831 editorFormElement.querySelector('legend').textContent = 'Add Bookmark'; | 843 editorFormElement.querySelector('legend').textContent = 'Add Bookmark'; |
832 editorFormElement.querySelector('input:not([type="hidden"])').accessKey = | 844 editorFormElement.querySelector('input:not([type="hidden"])').accessKey = |
833 'a'; | 845 'a'; |
846 editorFormElement.addEventListener('input', this); | |
834 editorFormElement.addEventListener('click', this); | 847 editorFormElement.addEventListener('click', this); |
835 editorFormElement.addEventListener('submit', this); | 848 editorFormElement.addEventListener('submit', this); |
836 editorFormElement.addEventListener('reset', this); | 849 editorFormElement.addEventListener('reset', this); |
837 | 850 |
838 this.editTagListElement = | 851 this.editTagListElement = |
839 editorFormElement.querySelector('ul.tag-input-list'); | 852 editorFormElement.querySelector('ul.tag-input-list'); |
840 this.editTagListElement.appendChild(this.createTagInputElement('')); | 853 this.editTagListElement.appendChild(this.createTagInputElement('')); |
841 | 854 |
842 saveFormElement.parentNode.insertBefore(newNode, | 855 saveFormElement.parentNode.insertBefore(newNode, |
843 saveFormElement.nextSibling); | 856 saveFormElement.nextSibling); |
857 | |
858 document.querySelector('a#bookmarklet-link').href = BOOKMARKLET_URI; | |
844 }; | 859 }; |
845 | 860 |
846 extend(ActionsView, ObservableMixin); | 861 extend(ActionsView, ObservableMixin); |
847 | 862 |
848 ActionsView.prototype.createTagInputElement = function (tag) { | 863 ActionsView.prototype.createTagInputElement = function (tag) { |
853 | 868 |
854 return newNode; | 869 return newNode; |
855 }; | 870 }; |
856 | 871 |
857 ActionsView.prototype.handleEvent = function (e) { | 872 ActionsView.prototype.handleEvent = function (e) { |
873 var bookmarkletData; | |
874 var parsedData; | |
858 var tags = []; | 875 var tags = []; |
859 var i; | 876 var i; |
860 | 877 |
861 switch (e.type) { | 878 switch (e.type) { |
879 case 'input': | |
880 if (e.target.name === 'bookmarklet-import') { | |
881 // get rid of any preceding text | |
882 bookmarkletData = e.target.value.replace(/^[^{]*/, ''); | |
883 | |
884 try { | |
885 parsedData = JSON.parse(bookmarkletData); | |
886 } catch (exception) { | |
887 return; | |
888 } | |
889 | |
890 if (isString(parsedData.url) && parsedData.url !== '') { | |
891 e.target.form.elements.url.value = parsedData.url; | |
892 } | |
893 if (isString(parsedData.title) && parsedData.title !== '') { | |
894 e.target.form.elements.title.value = parsedData.title; | |
895 } | |
896 } | |
897 break; | |
862 case 'click': | 898 case 'click': |
863 if (e.target.name === 'more-tags') { | 899 if (e.target.name === 'more-tags') { |
864 e.preventDefault(); | 900 e.preventDefault(); |
865 e.target.blur(); | 901 e.target.blur(); |
866 | 902 |