From 8e6e1f97b21b63766ecdc8937d6954278638f3fb Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 14 Mar 2015 01:45:38 +0300 Subject: [PATCH] Manager: "Edit" button re-uses existing tabs, shift-click opens a new window --- background.js | 22 ++++++++++++++++++++++ manage.js | 25 +++++++++++++++++++++++++ popup.js | 10 ++-------- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/background.js b/background.js index a5022cdb..4056abdb 100644 --- a/background.js +++ b/background.js @@ -33,6 +33,9 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { case "healthCheck": getDatabase(function() { sendResponse(true); }, function() { sendResponse(false); }); break; + case "openURL": + openURL(request); + break; } }); @@ -321,3 +324,22 @@ chrome.tabs.onAttached.addListener(function(tabId, data) { } }); }); + +function openURL(options) { + chrome.tabs.query({currentWindow: true, url: options.url}, function(tabs) { + // switch to an existing tab with the requested url + if (tabs.length) { + chrome.tabs.highlight({windowId: tabs[0].windowId, tabs: tabs[0].index}, function (window) {}); + } else { + delete options.method; + chrome.tabs.query({currentWindow: true, active: true}, function(tabs) { + // re-use an active new tab page + if (tabs.length && tabs[0].url.match(/^chrome:\/\/newtab\/?$/)) { + chrome.tabs.update(options); + } else { + chrome.tabs.create(options); + } + }); + } + }); +} diff --git a/manage.js b/manage.js index 5bad7721..41c3a57a 100644 --- a/manage.js +++ b/manage.js @@ -93,6 +93,31 @@ function createStyleElement(style) { } var editLink = e.querySelector(".style-edit-link"); editLink.setAttribute("href", editLink.getAttribute("href") + style.id); + editLink.addEventListener("click", function(event) { + if (!event.altKey) { + var left = event.button == 0, middle = event.button == 1, + shift = event.shiftKey, ctrl = event.ctrlKey; + var openWindow = left && shift && !ctrl; + var openBackgroundTab = (middle && !shift) || (left && ctrl && !shift); + var openForegroundTab = (middle && shift) || (left && ctrl && shift); + if (openWindow || openBackgroundTab || openForegroundTab) { + event.preventDefault(); + event.stopPropagation(); + var url = event.target.href || event.target.parentNode.href; + if (openWindow) { + var options = prefs.getPref('windowPosition', {}); + options.url = url; + chrome.windows.create(options); + } else { + chrome.extension.sendMessage({ + method: "openURL", + url: url, + active: openForegroundTab + }); + } + } + } + }); e.querySelector(".enable").addEventListener("click", function(event) { enable(event, true); }, false); e.querySelector(".disable").addEventListener("click", function(event) { enable(event, false); }, false); e.querySelector(".check-update").addEventListener("click", doCheckUpdate, false); diff --git a/popup.js b/popup.js index 63ecb5a0..10f578bf 100644 --- a/popup.js +++ b/popup.js @@ -156,14 +156,8 @@ function openLinkInTabOrWindow(event) { function openLink(event) { event.preventDefault(); - chrome.tabs.query({currentWindow: true, active: true}, function (tabs) { - if (tabs && tabs.length && tabs[0].url.match(/^chrome:\/\/newtab\/?$/)) { - chrome.tabs.update({url: event.target.href}); - close(); // close the popup - } else { - chrome.tabs.create({url: event.target.href}); - } - }); + chrome.extension.sendMessage({method: "openURL", url: event.target.href}); + close(); } function handleUpdate(style) {