use parseUrl in Backend

This commit is contained in:
siikamiika 2020-02-10 00:09:29 +02:00
parent 4508efb9a6
commit 56f1f8384d
3 changed files with 18 additions and 15 deletions

View File

@ -608,14 +608,16 @@ class Backend {
const queryString = new URLSearchParams(queryParams).toString(); const queryString = new URLSearchParams(queryParams).toString();
const url = `${baseUrl}?${queryString}`; const url = `${baseUrl}?${queryString}`;
const isTabMatch = (url2) => {
if (url2 === null || !url2.startsWith(baseUrl)) { return false; }
const {baseUrl: baseUrl2, queryParams: queryParams2} = parseUrl(url2);
return baseUrl2 === baseUrl && (queryParams2.mode === mode || (!queryParams2.mode && mode === 'existingOrNewTab'));
};
switch (mode) { switch (mode) {
case 'existingOrNewTab': case 'existingOrNewTab':
try { try {
const tab = await Backend._findTab(1000, (url2) => ( const tab = await Backend._findTab(1000, isTabMatch);
url2 !== null &&
url2.startsWith(baseUrl) &&
(url2.length === baseUrl.length || url2[baseUrl.length] === '?' || url2[baseUrl.length] === '#')
));
if (tab !== null) { if (tab !== null) {
await Backend._focusTab(tab); await Backend._focusTab(tab);
if (queryParams.query) { if (queryParams.query) {

View File

@ -49,7 +49,7 @@ class DisplaySearch extends Display {
try { try {
await this.initialize(); await this.initialize();
const {query='', mode=''} = DisplaySearch.parseQueryStringFromLocation(window.location.href); const {queryParams: {query='', mode=''}} = parseUrl(window.location.href);
if (this.search !== null) { if (this.search !== null) {
this.search.addEventListener('click', (e) => this.onSearch(e), false); this.search.addEventListener('click', (e) => this.onSearch(e), false);
@ -66,7 +66,7 @@ class DisplaySearch extends Display {
this.wanakanaEnable.checked = false; this.wanakanaEnable.checked = false;
} }
this.wanakanaEnable.addEventListener('change', (e) => { this.wanakanaEnable.addEventListener('change', (e) => {
const {query=''} = DisplaySearch.parseQueryStringFromLocation(window.location.href); const {queryParams: {query=''}} = parseUrl(window.location.href);
if (e.target.checked) { if (e.target.checked) {
window.wanakana.bind(this.query); window.wanakana.bind(this.query);
apiOptionsSet({general: {enableWanakana: true}}, this.getOptionsContext()); apiOptionsSet({general: {enableWanakana: true}}, this.getOptionsContext());
@ -157,7 +157,7 @@ class DisplaySearch extends Display {
} }
onPopState() { onPopState() {
const {query='', mode=''} = DisplaySearch.parseQueryStringFromLocation(window.location.href); const {queryParams: {query='', mode=''}} = parseUrl(window.location.href);
document.documentElement.dataset.searchMode = mode; document.documentElement.dataset.searchMode = mode;
this.setQuery(query); this.setQuery(query);
this.onSearchQueryUpdated(this.query.value, false); this.onSearchQueryUpdated(this.query.value, false);
@ -323,13 +323,6 @@ class DisplaySearch extends Display {
document.title = `${text} - Yomichan Search`; document.title = `${text} - Yomichan Search`;
} }
} }
static parseQueryStringFromLocation(url) {
const parsedUrl = new URL(url);
const parsedSearch = new URLSearchParams(parsedUrl.search);
return Array.from(parsedSearch.entries())
.reduce((a, [k, v]) => Object.assign({}, a, {[k]: v}), {});
}
} }
DisplaySearch.onKeyDownIgnoreKeys = { DisplaySearch.onKeyDownIgnoreKeys = {

View File

@ -128,6 +128,14 @@ function stringReverse(string) {
return string.split('').reverse().join('').replace(/([\uDC00-\uDFFF])([\uD800-\uDBFF])/g, '$2$1'); return string.split('').reverse().join('').replace(/([\uDC00-\uDFFF])([\uD800-\uDBFF])/g, '$2$1');
} }
function parseUrl(url) {
const parsedUrl = new URL(url);
const baseUrl = `${parsedUrl.origin}${parsedUrl.pathname}`;
const queryParams = Array.from(parsedUrl.searchParams.entries())
.reduce((a, [k, v]) => Object.assign({}, a, {[k]: v}), {});
return {baseUrl, queryParams};
}
/* /*
* Async utilities * Async utilities