Add copying to clipboard and replacing bookmarks

This commit is contained in:
Les De Ridder 2016-01-18 16:07:23 +01:00
parent 447b0b6db2
commit 997e826002
4 changed files with 59 additions and 20 deletions

View File

@ -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}));
});
});
});

View File

@ -23,6 +23,7 @@
"tabs",
"storage",
"http://*/*",
"https://*/*"
"https://*/*",
"bookmarks"
]
}

View File

@ -21,25 +21,30 @@
<br />
URL:
<input type="textbox" id="urlbox">
<input type="text" id="urlbox">
<br />
<br />
After upload:<br />
<input type="radio" name="behaviour" id="newtab" value="newtab" checked> Open new tab<br />
<input type="radio" name="behaviour" id="replacetab" value="replacetab"> Replace current tab
<input type="radio" name="tabbehaviour" id="newtab" value="newtab" checked> Open new tab<br />
<input type="radio" name="tabbehaviour" id="replacetab" value="replacetab"> Replace current tab
<br />
<br />
<input type="checkbox" id="copytoclipboard"> Copy URL to clipboard<br />
<input type="checkbox" id="replacebookmark"> Replace bookmark (if original URL is bookmarked)
<br />
<br />
<!--
TODO:
* Add URL to clipboard (checkbox)
* Replace old bookmark (checkbox)
* Disable blacklist (checkbox)
* Add imageboard thread archival support
* Add booru support
* Error checks and messages
-->
<button id="save">Save</button>

View File

@ -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 = '';