From d4b810345de5eb28615e17a912218ccaee4d33ab Mon Sep 17 00:00:00 2001 From: "Samuel W. Flint" Date: Sat, 1 Jul 2017 10:23:18 -0500 Subject: [PATCH 1/3] Added in Options and Storage --- manifest.json | 6 +++++- options.html | 43 +++++++++++++++++++++++++++++++++++++++++++ options.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 options.html create mode 100644 options.js diff --git a/manifest.json b/manifest.json index b2c6dba..a07a39b 100644 --- a/manifest.json +++ b/manifest.json @@ -10,7 +10,11 @@ "scripts": ["background.js"] }, - "permissions": ["activeTab"], + "permissions": ["activeTab", "storage"], + + "options_ui": { + "page": "options.html" + }, "browser_action": { "default_icon": "org-mode-unicorn.png" diff --git a/options.html b/options.html new file mode 100644 index 0000000..27f6435 --- /dev/null +++ b/options.html @@ -0,0 +1,43 @@ + + + + Org Capture Settings + + + + + + + + + + + + + + + + + +
+ Selected Template + + +
+ Unselected Template + + +
+ Use Old-Style Links? + + +
+ +
+ + + + + diff --git a/options.js b/options.js new file mode 100644 index 0000000..4b5d4bc --- /dev/null +++ b/options.js @@ -0,0 +1,36 @@ +// Saves options to chrome.storage.sync. +function save_options() { + var selTemp = document.getElementById('selTemplate').value; + var unselTemp = document.getElementById('unselTemplate').value; + var oldStyleP = document.getElementById('useOldstyle').checked; + chrome.storage.sync.set({ + selectedTemplate: selTemp, + unselectedTemplate: unselTemp, + useOldStyleLinks: oldStyleP + }, function() { + // Update status to let user know options were saved. + var status = document.getElementById('status'); + status.textContent = 'Options saved.'; + setTimeout(function() { + status.textContent = ''; + }, 750); + }); +} + +// Restores select box and checkbox state using the preferences +// stored in chrome.storage. +function restore_options() { + // Use default value color = 'red' and likesColor = true. + chrome.storage.sync.get({ + selectedTemplate: 'nql', + unselectedTemplate: 'b', + useOldStyleLinks: false + }, function(items) { + document.getElementById('unselTemplate').value = items.unselectedTemplate; + document.getElementById('selTemplate').value = items.selectedTemplate; + document.getElementById('useOldstyle').checked = items.useOldStyleLinks; + }); +} +document.addEventListener('DOMContentLoaded', restore_options); +document.getElementById('save').addEventListener('click', + save_options); From 0637a7281b87447e4de2849bac8251c8d4115482 Mon Sep 17 00:00:00 2001 From: "Samuel W. Flint" Date: Sat, 1 Jul 2017 10:28:05 -0500 Subject: [PATCH 2/3] Updated the capture functionality a bit --- capture.js | 54 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/capture.js b/capture.js index 3dbb05a..0db57c3 100644 --- a/capture.js +++ b/capture.js @@ -1,26 +1,38 @@ -var capture = function(){ - var replace_all = function(str, find, replace) { return str.replace(new RegExp(find, 'g'), replace); }; - var esc = function (text) { return replace_all(replace_all(replace_all(encodeURIComponent(text),"[(]",escape("(")),"[)]",escape(")")),"[']",escape("'")); }; - var selection = window.getSelection().toString(); - - var uri = 'org-protocol://'; - if (selection != "") - { - uri += 'capture:/p/'; - } - else - { - uri += 'capture:/L/' - }; +function replace_all(str, find, replace) { + return str.replace(new RegExp(find, 'g'), replace); +} - uri += encodeURIComponent(location.href) + '/' + - esc(document.title) ; +function escapeIt(text) { + return replace_all(replace_all(replace_all(encodeURIComponent(text), "[(]", escape("(")), + "[)]", escape(")")), + "[']" ,escape("'")); +} - if (selection != "") { uri += '/' + esc(selection); }; +function createCaptureURL(template, title, url, selection, oldStyle) { + if (oldStyle == true) + return "org-protocol://capture:/"+template+'/'+url+'/'+title + ((selection === '') ? '' : ('/' + selection)); + else + return "org-protocol://capture?template="+template+'&url='+url+'&title='+title+((selection === '') ? '' : ('&body=' + selection)); +} - console.log("Capturing the following URI with org-protocol:", uri); +function captureIt() { + var selection = window.getSelection().toString(); + var url = encodeURIComponent(location.href); + var title = escapeIt(document.title); - location.href=uri; -}; + chrome.storage.sync.get({ + selectedTemplate: 'nql', + unselectedTemplate: 'b', + useOldStyleLinks: false + }, function(items) { + var uri = ''; + if (selection != '') + uri = createCaptureURL(items.selectedTemplate, title, url, selection, items.useOldStyleLinks); + else + uri = createCaptureURL(items.unselectedTemplate, title, url, selection, items.useOldStyleLinks); + alert(uri); + location.href = uri; + }); +} -capture(); +captureIt(); From 5d2cc5036f116adff2825146c5b7c8ceb741ecdd Mon Sep 17 00:00:00 2001 From: "Samuel W. Flint" Date: Mon, 3 Jul 2017 16:03:06 -0500 Subject: [PATCH 3/3] Updated default templates --- capture.js | 4 ++-- options.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/capture.js b/capture.js index 0db57c3..ed265ab 100644 --- a/capture.js +++ b/capture.js @@ -21,8 +21,8 @@ function captureIt() { var title = escapeIt(document.title); chrome.storage.sync.get({ - selectedTemplate: 'nql', - unselectedTemplate: 'b', + selectedTemplate: 'p', + unselectedTemplate: 'L', useOldStyleLinks: false }, function(items) { var uri = ''; diff --git a/options.js b/options.js index 4b5d4bc..62bfcef 100644 --- a/options.js +++ b/options.js @@ -22,8 +22,8 @@ function save_options() { function restore_options() { // Use default value color = 'red' and likesColor = true. chrome.storage.sync.get({ - selectedTemplate: 'nql', - unselectedTemplate: 'b', + selectedTemplate: 'L', + unselectedTemplate: 'p', useOldStyleLinks: false }, function(items) { document.getElementById('unselTemplate').value = items.unselectedTemplate;