From d98166098309a7e7eddc470dbc24a442a99e5f77 Mon Sep 17 00:00:00 2001 From: eight Date: Wed, 1 Nov 2017 09:26:53 +0800 Subject: [PATCH] Fix: drop getParams --- edit/edit.js | 25 ++++++------------------- install-usercss/install-usercss.js | 23 +++-------------------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/edit/edit.js b/edit/edit.js index d972fc31..20f99455 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -1297,7 +1297,6 @@ onDOMready().then(init); function init() { initCodeMirror(); - const params = getParams(); getStyle().then(style => { styleId = style.id; sessionStorage.justEditedStyleId = styleId; @@ -1310,7 +1309,8 @@ function init() { }); function getStyle() { - if (!params.id) { + const id = new URLSearchParams(location.search).get('id'); + if (!id) { // match should be 2 - one for the whole thing, one for the parentheses // This is an add $('#heading').textContent = t('addStyleTitle'); @@ -1318,7 +1318,7 @@ function init() { } $('#heading').textContent = t('editStyleHeading'); // This is an edit - return getStylesSafe({id: params.id}).then(styles => { + return getStylesSafe({id}).then(styles => { let style = styles[0]; if (!style) { style = createEmptyStyle(); @@ -1329,7 +1329,7 @@ function init() { } function createEmptyStyle() { - const params = getParams(); + const params = new URLSearchParams(location.search); const style = { id: null, name: '', @@ -1337,8 +1337,8 @@ function init() { sections: [{code: ''}] }; for (const i in CssToProperty) { - if (params[i]) { - style.sections[0][CssToProperty[i]] = [params[i]]; + if (params.get(i)) { + style.sections[0][CssToProperty[i]] = [params.get(i)]; } } return style; @@ -1893,19 +1893,6 @@ function showCodeMirrorPopup(title, html, options) { return popup; } -function getParams() { - const params = {}; - const urlParts = location.href.split('?', 2); - if (urlParts.length === 1) { - return params; - } - urlParts[1].split('&').forEach(keyValue => { - const splitKeyValue = keyValue.split('=', 2); - params[decodeURIComponent(splitKeyValue[0])] = decodeURIComponent(splitKeyValue[1]); - }); - return params; -} - chrome.runtime.onMessage.addListener(onRuntimeMessage); function replaceStyle(request) { diff --git a/install-usercss/install-usercss.js b/install-usercss/install-usercss.js index 0a8846d4..362b93e1 100644 --- a/install-usercss/install-usercss.js +++ b/install-usercss/install-usercss.js @@ -2,12 +2,12 @@ 'use strict'; (() => { - const params = getParams(); + const params = new URLSearchParams(location.search); let liveReload = false; let installed = false; const port = chrome.tabs.connect( - Number(params.tabId), + Number(params.get('tabId')), {name: 'usercss-install', frameId: 0} ); port.postMessage({method: 'getSourceCode'}); @@ -234,7 +234,7 @@ // set updateUrl const setUpdate = $('.set-update-url input[type=checkbox]'); - const updateUrl = new URL(params.updateUrl); + const updateUrl = new URL(params.get('updateUrl')); $('.set-update-url > span').textContent = t('installUpdateFromLabel', updateUrl.href); if (dup && dup.updateUrl === updateUrl.href) { setUpdate.checked = true; @@ -272,23 +272,6 @@ } } - function getParams() { - // URL.searchParams needs chrome 51+ - const {search} = location; - const result = {}; - for (const param of search.slice(1).split('&')) { - let key, value; - if (param.includes('=')) { - [key, value] = param.split('=').map(decodeURIComponent); - } else { - key = decodeURIComponent(param); - value = true; - } - result[key] = value; - } - return result; - } - function getAppliesTo(style) { function *_gen() { for (const section of style.sections) {