From 0a865318a418371db6b1438f128c9271f43995e3 Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 10 Dec 2017 07:56:41 +0300 Subject: [PATCH] popup: "configure" opens USO page and expands the settings area --- content/install-hook-userstyles.js | 32 +++++++++++++++++++++++++++--- js/messaging.js | 4 ++-- popup/popup.js | 20 ++++++++++++++++++- popup/search-results.js | 1 + 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/content/install-hook-userstyles.js b/content/install-hook-userstyles.js index cdafd311..e533b03a 100644 --- a/content/install-hook-userstyles.js +++ b/content/install-hook-userstyles.js @@ -13,9 +13,15 @@ document.addEventListener('stylish' + type + browser, onClick))); chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { - // orphaned content script check - if (msg.method === 'ping') { - sendResponse(true); + switch (msg.method) { + case 'ping': + // orphaned content script check + sendResponse(true); + break; + case 'openSettings': + openSettings(); + sendResponse(true); + break; } }); @@ -260,6 +266,26 @@ } + function openSettings(countdown = 10e3) { + const button = document.querySelector('.advanced_button') || + document.evaluate('//*[not(*) and contains(., "Advanced Style Settings")]', + document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; + if (button) { + button.dispatchEvent(new MouseEvent('click', {bubbles: true})); + setTimeout(function pollArea(countdown = 2000) { + const area = document.getElementById('advancedsettings_area'); + if (area || countdown < 0) { + (area || button).scrollIntoView({behavior: 'smooth', block: area ? 'end' : 'center'}); + } else { + setTimeout(pollArea, 100, countdown - 100); + } + }, 500); + } else if (countdown > 0) { + setTimeout(openSettings, 100, countdown - 100); + } + } + + function orphanCheck() { if (chrome.i18n && chrome.i18n.getUILanguage()) { return true; diff --git a/js/messaging.js b/js/messaging.js index 8351a4c0..1feb35be 100644 --- a/js/messaging.js +++ b/js/messaging.js @@ -213,7 +213,7 @@ function getTabRealURL(tab) { // opens a tab or activates the already opened one, // reuses the New Tab page if it's focused now -function openURL({url, index, openerTabId, currentWindow = true}) { +function openURL({url, index, active, currentWindow = true}) { if (!url.includes('://')) { url = chrome.runtime.getURL(url); } @@ -237,7 +237,7 @@ function openURL({url, index, openerTabId, currentWindow = true}) { chrome.tabs.update({url}, resolve); } else { // create a new tab - const options = {url, index}; + const options = {url, index, active}; // FF57+ supports openerTabId, but not in Android (indicated by the absence of chrome.windows) if (tab && (!FIREFOX || FIREFOX >= 57 && chrome.windows) && !chromeInIncognito) { options.openerTabId = tab.id; diff --git a/popup/popup.js b/popup/popup.js index 8252aceb..9fb0fed6 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -277,6 +277,7 @@ function createStyleElement({ config.href = style.url; config.target = '_blank'; config.title = t('configureStyleOnHomepage'); + config.dataset.sendMessage = JSON.stringify({method: 'openSettings'}); $('use', config).attributes['xlink:href'].nodeValue = '#svg-icon-config-uso'; } else if (!style.usercssData || !Object.keys(style.usercssData.vars || {}).length) { config.style.display = 'none'; @@ -364,6 +365,8 @@ Object.assign(handleEvent, { hotkeys.setState(true); }); }); + } else { + handleEvent.openURLandHide.call(this, event); } }, @@ -418,7 +421,22 @@ Object.assign(handleEvent, { openURLandHide(event) { event.preventDefault(); - openURL({url: this.href || this.dataset.href}) + const el = this; + const msg = tryJSONparse(el.dataset.sendMessage); + getActiveTab() + .then(activeTab => openURL({ + url: this.href || this.dataset.href, + index: activeTab.index + 1, + active: false, + })) + .then(msg && ( + function poll(tab, t0 = performance.now()) { + msg.tabId = tab.id; + return sendMessage(msg) + .catch(ignoreChromeError) + .then(handled => handled || performance.now() - t0 < 200 && poll(tab)) + .then(() => chrome.tabs.update(tab.id, {active: true})); + })) .then(window.close); }, diff --git a/popup/search-results.js b/popup/search-results.js index e232b608..4984b0eb 100755 --- a/popup/search-results.js +++ b/popup/search-results.js @@ -366,6 +366,7 @@ window.addEventListener('showStyles:done', function _() { const customizeButton = $('.search-result-customize', entry); customizeButton.dataset.href = searchAPI.BASE_URL + result.url; + customizeButton.dataset.sendMessage = JSON.stringify({method: 'openSettings'}); customizeButton.classList.remove('hidden'); customizeButton.onclick = function (event) { event.stopPropagation();