remove URLSearchParams workaround, not needed since Chrome 55

This commit is contained in:
tophf 2020-10-12 09:59:09 +03:00
parent 58094f80cd
commit 4df3813f87
7 changed files with 30 additions and 41 deletions

View File

@ -297,13 +297,10 @@ function openEditor(params) {
'url-prefix'?: String 'url-prefix'?: String
} }
*/ */
const searchParams = new URLSearchParams(); const u = new URL(chrome.runtime.getURL('edit.html'));
for (const key in params) { u.search = new URLSearchParams(params);
searchParams.set(key, params[key]);
}
const search = searchParams.toString();
return openURL({ return openURL({
url: 'edit.html' + (search && `?${search}`), url: `${u}`,
newWindow: prefs.get('openEditInWindow'), newWindow: prefs.get('openEditInWindow'),
windowPosition: prefs.get('windowPosition'), windowPosition: prefs.get('windowPosition'),
currentWindow: null currentWindow: null

View File

@ -37,7 +37,7 @@ const tokenManager = (() => {
scopes: ['https://www.googleapis.com/auth/drive.appdata'], scopes: ['https://www.googleapis.com/auth/drive.appdata'],
revoke: token => { revoke: token => {
const params = {token}; const params = {token};
return postQuery(`https://accounts.google.com/o/oauth2/revoke?${stringifyQuery(params)}`); return postQuery(`https://accounts.google.com/o/oauth2/revoke?${new URLSearchParams(params)}`);
} }
}, },
onedrive: { onedrive: {
@ -137,14 +137,6 @@ const tokenManager = (() => {
}); });
} }
function stringifyQuery(obj) {
const search = new URLSearchParams();
for (const key of Object.keys(obj)) {
search.set(key, obj[key]);
}
return search.toString();
}
function authUser(name, k, interactive = false) { function authUser(name, k, interactive = false) {
const provider = AUTH[name]; const provider = AUTH[name];
const state = Math.random().toFixed(8).slice(2); const state = Math.random().toFixed(8).slice(2);
@ -160,7 +152,7 @@ const tokenManager = (() => {
if (provider.authQuery) { if (provider.authQuery) {
Object.assign(query, provider.authQuery); Object.assign(query, provider.authQuery);
} }
const url = `${provider.authURL}?${stringifyQuery(query)}`; const url = `${provider.authURL}?${new URLSearchParams(query)}`;
return webextLaunchWebAuthFlow({ return webextLaunchWebAuthFlow({
url, url,
interactive, interactive,
@ -211,11 +203,9 @@ const tokenManager = (() => {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Type': 'application/x-www-form-urlencoded'
} },
body: body ? new URLSearchParams(body) : null,
}; };
if (body) {
options.body = stringifyQuery(body);
}
return fetch(url, options) return fetch(url, options)
.then(r => { .then(r => {
if (r.ok) { if (r.ok) {

View File

@ -396,8 +396,7 @@ document.documentElement.appendChild(document.createElement('script')).text = '(
const originalResponseJson = Response.prototype.json; const originalResponseJson = Response.prototype.json;
document.addEventListener('stylusFixBuggyUSOsettings', function _({detail}) { document.addEventListener('stylusFixBuggyUSOsettings', function _({detail}) {
document.removeEventListener('stylusFixBuggyUSOsettings', _); document.removeEventListener('stylusFixBuggyUSOsettings', _);
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425) settings = /\?/.test(detail) && new URL(detail).searchParams;
settings = /\?/.test(detail) && new URLSearchParams(new URL(detail).search.replace(/^\?/, ''));
if (!settings) { if (!settings) {
Response.prototype.json = originalResponseJson; Response.prototype.json = originalResponseJson;
} }

View File

@ -352,8 +352,7 @@ function isUsercss(style) {
} }
function initStyleData() { function initStyleData() {
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425) const params = new URLSearchParams(location.search);
const params = new URLSearchParams(location.search.replace(/^\?/, ''));
const id = Number(params.get('id')); const id = Number(params.get('id'));
const createEmptyStyle = () => ({ const createEmptyStyle = () => ({
name: params.get('domain') || name: params.get('domain') ||

View File

@ -3,8 +3,7 @@
'use strict'; 'use strict';
(() => { (() => {
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425) const params = new URLSearchParams(location.search);
const params = new URLSearchParams(location.search.replace(/^\?/, ''));
const tabId = params.has('tabId') ? Number(params.get('tabId')) : -1; const tabId = params.has('tabId') ? Number(params.get('tabId')) : -1;
const initialUrl = params.get('updateUrl'); const initialUrl = params.get('updateUrl');

View File

@ -62,5 +62,20 @@ self.INJECTED !== 1 && (() => {
} }
} }
if (!(new URLSearchParams({foo: 1})).get('foo')) {
// TODO: remove when minimum_chrome_version >= 61
window.URLSearchParams = class extends URLSearchParams {
constructor(init) {
if (init && typeof init === 'object') {
super();
for (const [key, val] of Object.entries(init)) {
this.set(key, val);
}
} else {
super(...arguments);
}
}
};
}
//#endregion //#endregion
})(); })();

View File

@ -31,18 +31,9 @@ const router = (() => {
} }
function updateSearch(key, value) { function updateSearch(key, value) {
const search = new URLSearchParams(location.search.replace(/^\?/, '')); const u = new URL(location);
if (!value) { u.searchParams[value ? 'set' : 'delete'](key, value);
search.delete(key); history.replaceState(history.state, null, `${u}`);
} else {
search.set(key, value);
}
const finalSearch = search.toString();
if (finalSearch) {
history.replaceState(history.state, null, `?${finalSearch}${location.hash}`);
} else {
history.replaceState(history.state, null, `${location.pathname}${location.hash}`);
}
update(true); update(true);
} }
@ -66,7 +57,7 @@ const router = (() => {
} }
function getSearch(key) { function getSearch(key) {
return new URLSearchParams(location.search.replace(/^\?/, '')).get(key); return new URLSearchParams(location.search).get(key);
} }
function update(replace) { function update(replace) {
@ -86,8 +77,7 @@ const router = (() => {
if (options.hash) { if (options.hash) {
state = options.hash === location.hash; state = options.hash === location.hash;
} else if (options.search) { } else if (options.search) {
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425) const search = new URLSearchParams(location.search);
const search = new URLSearchParams(location.search.replace(/^\?/, ''));
state = options.search.map(key => search.get(key)); state = options.search.map(key => search.get(key));
} }
if (!deepEqual(state, options.currentState)) { if (!deepEqual(state, options.currentState)) {