From 01329b7835fa17642dd13a17406479c8b4e4b07d Mon Sep 17 00:00:00 2001 From: Jason Barnabe Date: Fri, 30 Jan 2015 12:35:37 -0600 Subject: [PATCH] Remember position of edit window #7 --- background.js | 13 +++++++++++++ popup.js | 12 ++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/background.js b/background.js index 27d0f35e..24dcf498 100644 --- a/background.js +++ b/background.js @@ -297,3 +297,16 @@ function saveFromJSONStyleReloaded(updateType, style, callback) { // Get the DB so that any first run actions will be performed immediately when the background page loads. getDatabase(function() {}, reportError); + +// When an edit page gets attached or detached, remember its state so we can do the same to the next one to open. +var editFullUrl = chrome.extension.getURL("edit.html"); +chrome.tabs.onAttached.addListener(function(tabId, data) { + chrome.tabs.get(tabId, function(tabData) { + if (tabData.url.indexOf(editFullUrl) == 0) { + chrome.windows.get(tabData.windowId, {populate: true}, function(win) { + // If there's only one tab in this window, it's been dragged to new window + localStorage['openEditInWindow'] = win.tabs.length == 1 ? "true" : "false"; + }); + } + }); +}); diff --git a/popup.js b/popup.js index 71cd47c5..3c05ff25 100644 --- a/popup.js +++ b/popup.js @@ -46,7 +46,7 @@ chrome.tabs.getSelected(null, function(tab) { if (index > 0) { writeStyle.appendChild(document.createTextNode(" ")); } - link.addEventListener("click", openLink, false); + link.addEventListener("click", openLinkInTabOrWindow, false); writeStyle.appendChild(link); }); }); @@ -117,10 +117,18 @@ function getId(event) { return null; } +function openLinkInTabOrWindow(event) { + event.preventDefault(); + if (localStorage['openEditInWindow'] == 'true') { + chrome.windows.create({url: event.target.href}); + } else { + chrome.tabs.create({url: event.target.href}); + } +} + function openLink(event) { event.preventDefault(); chrome.tabs.create({url: event.target.href}); - //return false; } function handleUpdate(style) {