From 997e826002b526a972691cedcb5649b7afa5642c Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Mon, 18 Jan 2016 16:07:23 +0100 Subject: [PATCH] Add copying to clipboard and replacing bookmarks --- background.js | 34 +++++++++++++++++++++++++++++----- manifest.json | 3 ++- options.html | 15 ++++++++++----- options.js | 27 ++++++++++++++++++--------- 4 files changed, 59 insertions(+), 20 deletions(-) diff --git a/background.js b/background.js index 9b63519..0016bfc 100644 --- a/background.js +++ b/background.js @@ -13,12 +13,28 @@ function getCurrentTabUrl(callback) { }); } +function replaceBookmark(oldUrl, newUrl) { + chrome.bookmarks.search({url: oldUrl}, function(bookmarks) { + for(bookmark of bookmarks) { + chrome.bookmarks.update(bookmark.id, {url: newUrl}); + } + }); +} + +function copyToClipboard(url) { + document.oncopy = function(event) { + event.clipboardData.setData("text/plain", url); + event.preventDefault(); + }; + document.execCommand('Copy', false, null); +} + chrome.pageAction.onClicked.addListener(function(tab) { getCurrentTabUrl(function(url) { var filename = url.substr(url.lastIndexOf("/")); - chrome.storage.sync.get({url: '', behaviour: ''}, function(items) { - if(items.url == '') { + chrome.storage.sync.get({url: '', tabbehaviour: '', copytoclipboard: false, replacebookmark: false}, function(config) { + if(config.url == '') { alert("Please select a Pomf clone."); chrome.tabs.create({ url: "options.html" }); return; @@ -27,10 +43,10 @@ chrome.pageAction.onClicked.addListener(function(tab) { var worker = new Worker('worker.js'); worker.onmessage = function(event) { var response = JSON.parse(event.data); - + var newUrl = response.files[0].url; - switch(items.behaviour) + switch(config.tabbehaviour) { case "newtab": chrome.tabs.create({url: newUrl}); @@ -39,8 +55,16 @@ chrome.pageAction.onClicked.addListener(function(tab) { chrome.tabs.update({url: newUrl}); break; } + + if(config.copytoclipboard) { + copyToClipboard(newUrl); + } + + if(config.replacebookmark) { + replaceBookmark(url, newUrl); + } }; - worker.postMessage(JSON.stringify({pomfclone: items.url, url: url, filename: filename})); + worker.postMessage(JSON.stringify({pomfclone: config.url, url: url, filename: filename})); }); }); }); diff --git a/manifest.json b/manifest.json index 40c02ae..819abf7 100644 --- a/manifest.json +++ b/manifest.json @@ -23,6 +23,7 @@ "tabs", "storage", "http://*/*", - "https://*/*" + "https://*/*", + "bookmarks" ] } diff --git a/options.html b/options.html index f05a5e5..9aec2fd 100644 --- a/options.html +++ b/options.html @@ -21,25 +21,30 @@
URL: - +

After upload:
- Open new tab
- Replace current tab + Open new tab
+ Replace current tab + +
+
+ + Copy URL to clipboard
+ Replace bookmark (if original URL is bookmarked)

diff --git a/options.js b/options.js index 92520ee..cdc0fc5 100644 --- a/options.js +++ b/options.js @@ -26,13 +26,17 @@ function setUrlBox() { function saveOptions() { var pomfclone = document.getElementById('pomfclone').value; var url = document.getElementById('urlbox').value; - var behaviour = document.querySelector('input[name="behaviour"]:checked').value; + var tabbehaviour = document.querySelector('input[name="tabbehaviour"]:checked').value; + var copytoclipboard = document.getElementById('copytoclipboard').checked; + var replacebookmark = document.getElementById('replacebookmark').checked; chrome.storage.sync.set({ pomfclone: pomfclone, url: url, customUrl: customUrl, - behaviour: behaviour + tabbehaviour: tabbehaviour, + copytoclipboard: copytoclipboard, + replacebookmark: replacebookmark }, function() { var status = document.getElementById('status'); status.textContent = 'Options saved.'; @@ -49,15 +53,20 @@ function restoreOptions() { pomfclone: firstPomfclone.innerHTML, url: firstPomfclone.value, customUrl: '', - behaviour: document.querySelector('input[name="behaviour"]:checked').value - }, function(items) { - document.getElementById('pomfclone').value = items.pomfclone; - document.getElementById('urlbox').value = items.url; - customUrl = items.customUrl; - custom = items.pomfclone == "custom"; + tabbehaviour: document.querySelector('input[name="tabbehaviour"]:checked').value, + copytoclipboard: false, + replacebookmark: false + }, function(config) { + document.getElementById('pomfclone').value = config.pomfclone; + document.getElementById('urlbox').value = config.url; + customUrl = config.customUrl; + custom = config.pomfclone == "custom"; setUrlBox(); - document.getElementById(items.behaviour).checked = true; + document.getElementById(config.tabbehaviour).checked = true; + + document.getElementById('copytoclipboard').checked = config.copytoclipboard; + document.getElementById('replacebookmark').checked = config.replacebookmark; }); document.getElementById('status').textContent = '';