Breaking: add router, keep search params
This commit is contained in:
parent
323103f55a
commit
b4662f1523
|
@ -265,7 +265,11 @@ const APPLY = (() => {
|
||||||
if (IS_OWN_PAGE) {
|
if (IS_OWN_PAGE) {
|
||||||
// FIXME: currently we only do this in our own page. Is it safe to do
|
// FIXME: currently we only do this in our own page. Is it safe to do
|
||||||
// it on all pages?
|
// it on all pages?
|
||||||
history.pushState({preUrl: location.href}, null, ' ');
|
try {
|
||||||
|
// history.replaceState(null, null, ' ');
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
router.updateHash('');
|
||||||
|
} catch (err) {}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
74
js/router.js
Normal file
74
js/router.js
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
/* global deepEqual */
|
||||||
|
/* exported router */
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const router = (() => {
|
||||||
|
// FIXME: this only works with one history
|
||||||
|
const buffer = [];
|
||||||
|
const watchers = [];
|
||||||
|
document.addEventListener('DOMContentLoaded', () => update());
|
||||||
|
window.addEventListener('popstate', () => update());
|
||||||
|
window.addEventListener('hashchange', () => update());
|
||||||
|
return {watch, updateSearch, getSearch, updateHash};
|
||||||
|
|
||||||
|
function watch(options, callback) {
|
||||||
|
watchers.push({options, callback});
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateSearch(key, value) {
|
||||||
|
const search = new URLSearchParams(location.search.replace(/^\?/, ''));
|
||||||
|
if (!value) {
|
||||||
|
search.delete(key);
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateHash(hash) {
|
||||||
|
if (buffer.length > 1) {
|
||||||
|
if (!hash && !buffer[buffer.length - 2].includes('#') || buffer[buffer.length - 2].endsWith(hash)) {
|
||||||
|
buffer.pop();
|
||||||
|
history.back();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hash) {
|
||||||
|
hash = ' ';
|
||||||
|
}
|
||||||
|
history.pushState(history.state, null, hash);
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSearch(key) {
|
||||||
|
return new URLSearchParams(location.search.replace(/^\?/, '')).get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
function update(replace) {
|
||||||
|
if (!buffer.length || buffer[buffer.length - 1] !== location.href && !replace) {
|
||||||
|
buffer.push(location.href);
|
||||||
|
} else if (buffer.length && replace) {
|
||||||
|
buffer[buffer.length - 1] = location.href;
|
||||||
|
}
|
||||||
|
for (const {options, callback} of watchers) {
|
||||||
|
let state;
|
||||||
|
if (options.hash) {
|
||||||
|
state = options.hash === location.hash;
|
||||||
|
} else if (options.search) {
|
||||||
|
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425)
|
||||||
|
const search = new URLSearchParams(location.search.replace(/^\?/, ''));
|
||||||
|
state = options.search.map(key => search.get(key));
|
||||||
|
}
|
||||||
|
if (!deepEqual(state, options.currentState)) {
|
||||||
|
options.currentState = state;
|
||||||
|
callback(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
|
@ -150,6 +150,7 @@
|
||||||
<script src="js/promisify.js"></script>
|
<script src="js/promisify.js"></script>
|
||||||
<script src="js/dom.js"></script>
|
<script src="js/dom.js"></script>
|
||||||
<script src="js/messaging.js"></script>
|
<script src="js/messaging.js"></script>
|
||||||
|
<script src="js/router.js"></script>
|
||||||
<script src="js/prefs.js"></script>
|
<script src="js/prefs.js"></script>
|
||||||
<script src="js/msg.js"></script>
|
<script src="js/msg.js"></script>
|
||||||
<script src="content/style-injector.js"></script>
|
<script src="content/style-injector.js"></script>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* global installed messageBox sorter $ $$ $create t debounce prefs API onDOMready */
|
/* global installed messageBox sorter $ $$ $create t debounce prefs API router */
|
||||||
/* exported filterAndAppend */
|
/* exported filterAndAppend */
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
@ -9,11 +9,18 @@ const filtersSelector = {
|
||||||
numTotal: 0,
|
numTotal: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: remove .replace(/^\?/, '') when minimum_chrome_version >= 52 (https://crbug.com/601425)
|
let initialized = false;
|
||||||
const urlFilterParam = new URLSearchParams(location.search.replace(/^\?/, '')).get('url');
|
|
||||||
if (location.search) {
|
router.watch({search: ['search']}, ([search]) => {
|
||||||
history.replaceState(0, document.title, location.origin + location.pathname);
|
if (search != null) {
|
||||||
}
|
$('#search').value = search;
|
||||||
|
}
|
||||||
|
if (!initialized) {
|
||||||
|
init();
|
||||||
|
} else {
|
||||||
|
searchStyles();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
HTMLSelectElement.prototype.adjustWidth = function () {
|
HTMLSelectElement.prototype.adjustWidth = function () {
|
||||||
const option0 = this.selectedOptions[0];
|
const option0 = this.selectedOptions[0];
|
||||||
|
@ -30,11 +37,11 @@ HTMLSelectElement.prototype.adjustWidth = function () {
|
||||||
parent.replaceChild(this, singleSelect);
|
parent.replaceChild(this, singleSelect);
|
||||||
};
|
};
|
||||||
|
|
||||||
onDOMready().then(() => {
|
function init() {
|
||||||
$('#search').oninput = searchStyles;
|
$('#search').oninput = e => {
|
||||||
if (urlFilterParam) {
|
router.updateSearch('search', e.target.value);
|
||||||
$('#search').value = 'url:' + urlFilterParam;
|
};
|
||||||
}
|
|
||||||
$('#search-help').onclick = event => {
|
$('#search-help').onclick = event => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
messageBox({
|
messageBox({
|
||||||
|
@ -120,6 +127,7 @@ onDOMready().then(() => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filterOnChange({forceRefilter: true});
|
filterOnChange({forceRefilter: true});
|
||||||
|
router.updateSearch('search', '');
|
||||||
};
|
};
|
||||||
|
|
||||||
// Adjust width after selects are visible
|
// Adjust width after selects are visible
|
||||||
|
@ -130,8 +138,10 @@ onDOMready().then(() => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
|
||||||
filterOnChange({forceRefilter: true});
|
filterOnChange({forceRefilter: true});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
function filterOnChange({target: el, forceRefilter}) {
|
function filterOnChange({target: el, forceRefilter}) {
|
||||||
|
@ -271,7 +281,7 @@ function showFiltersStats() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function searchStyles({immediately, container}) {
|
function searchStyles({immediately, container} = {}) {
|
||||||
const el = $('#search');
|
const el = $('#search');
|
||||||
const query = el.value.trim();
|
const query = el.value.trim();
|
||||||
if (query === el.lastValue && !immediately && !container) {
|
if (query === el.lastValue && !immediately && !container) {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
/*
|
/*
|
||||||
global messageBox getStyleWithNoCode
|
global messageBox getStyleWithNoCode
|
||||||
filterAndAppend urlFilterParam showFiltersStats
|
filterAndAppend showFiltersStats
|
||||||
checkUpdate handleUpdateInstalled
|
checkUpdate handleUpdateInstalled
|
||||||
objectDiff
|
objectDiff
|
||||||
configDialog
|
configDialog
|
||||||
sorter msg prefs API onDOMready $ $$ $create template setupLivePrefs
|
sorter msg prefs API onDOMready $ $$ $create template setupLivePrefs
|
||||||
URLS enforceInputRange t tWordBreak formatDate
|
URLS enforceInputRange t tWordBreak formatDate
|
||||||
getOwnTab getActiveTab openURL animateElement sessionStorageHash debounce
|
getOwnTab getActiveTab openURL animateElement sessionStorageHash debounce
|
||||||
scrollElementIntoView CHROME VIVALDI FIREFOX
|
scrollElementIntoView CHROME VIVALDI FIREFOX router
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
@ -35,7 +35,8 @@ const handleEvent = {};
|
||||||
|
|
||||||
Promise.all([
|
Promise.all([
|
||||||
API.getAllStyles(true),
|
API.getAllStyles(true),
|
||||||
urlFilterParam && API.searchDB({query: 'url:' + urlFilterParam}),
|
// FIXME: integrate this into filter.js
|
||||||
|
router.getSearch('search') && API.searchDB({query: router.getSearch('search')}),
|
||||||
Promise.all([
|
Promise.all([
|
||||||
onDOMready(),
|
onDOMready(),
|
||||||
prefs.initializing,
|
prefs.initializing,
|
||||||
|
@ -58,10 +59,6 @@ Promise.all([
|
||||||
msg.onExtension(onRuntimeMessage);
|
msg.onExtension(onRuntimeMessage);
|
||||||
|
|
||||||
function onRuntimeMessage(msg) {
|
function onRuntimeMessage(msg) {
|
||||||
if (msg === 'options-open' || msg === 'options-close') {
|
|
||||||
toggleOptions(msg);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
switch (msg.method) {
|
switch (msg.method) {
|
||||||
case 'styleUpdated':
|
case 'styleUpdated':
|
||||||
case 'styleAdded':
|
case 'styleAdded':
|
||||||
|
@ -84,8 +81,9 @@ function onRuntimeMessage(msg) {
|
||||||
function initGlobalEvents() {
|
function initGlobalEvents() {
|
||||||
installed = $('#installed');
|
installed = $('#installed');
|
||||||
installed.onclick = handleEvent.entryClicked;
|
installed.onclick = handleEvent.entryClicked;
|
||||||
$('#manage-options-button').onclick = () =>
|
$('#manage-options-button').onclick = () => {
|
||||||
history.replaceState('', document.title, location.href + '#stylus-options');
|
router.updateHash('#stylus-options');
|
||||||
|
};
|
||||||
{
|
{
|
||||||
const btn = $('#manage-shortcuts-button');
|
const btn = $('#manage-shortcuts-button');
|
||||||
btn.onclick = btn.onclick || (() => openURL({url: URLS.configureCommands}));
|
btn.onclick = btn.onclick || (() => openURL({url: URLS.configureCommands}));
|
||||||
|
@ -717,41 +715,31 @@ function embedOptions() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unembedOptions() {
|
||||||
function removeOptions() {
|
|
||||||
const options = $('#stylus-embedded-options');
|
const options = $('#stylus-embedded-options');
|
||||||
if (options) options.remove();
|
if (options) {
|
||||||
}
|
options.contentWindow.document.body.classList.add('scaleout');
|
||||||
|
options.classList.add('fadeout');
|
||||||
|
animateElement(options, {
|
||||||
|
className: 'fadeout',
|
||||||
|
onComplete: removeOptions,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// wait for possible filter params to be removed
|
function removeOptions() {
|
||||||
onDOMready().then(() => {
|
const options = $('#stylus-embedded-options');
|
||||||
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
|
if (options) options.remove();
|
||||||
if (changeInfo.url) {
|
|
||||||
if (location.hash === '#stylus-options') {
|
|
||||||
embedOptions();
|
|
||||||
} else {
|
|
||||||
const options = $('#stylus-embedded-options');
|
|
||||||
if (options) {
|
|
||||||
options.contentWindow.document.body.classList.add('scaleout');
|
|
||||||
options.classList.add('fadeout');
|
|
||||||
animateElement(options, {
|
|
||||||
className: 'fadeout',
|
|
||||||
onComplete: removeOptions,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
function toggleOptions(msg) {
|
|
||||||
if (msg === 'options-open' && location.hash !== '#stylus-options') {
|
|
||||||
history.replaceState('', document.title, location.href + '#stylus-options');
|
|
||||||
} else if (msg === 'options-close' && location.hash === '#stylus-options') {
|
|
||||||
history.replaceState('', document.title, location.origin + location.pathname);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
router.watch({hash: '#stylus-options'}, state => {
|
||||||
|
if (state) {
|
||||||
|
embedOptions();
|
||||||
|
} else {
|
||||||
|
unembedOptions();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (location.hash === '#stylus-options') embedOptions();
|
window.addEventListener('closeOptions', () => {
|
||||||
|
router.updateHash('');
|
||||||
|
});
|
||||||
|
|
|
@ -40,7 +40,7 @@ if (FIREFOX && 'update' in (chrome.commands || {})) {
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
$('#options-close-icon').onclick = () => {
|
$('#options-close-icon').onclick = () => {
|
||||||
top.history.replaceState('', top.document.title, top.location.origin + top.location.pathname);
|
top.dispatchEvent(new CustomEvent('closeOptions'));
|
||||||
};
|
};
|
||||||
|
|
||||||
document.onclick = e => {
|
document.onclick = e => {
|
||||||
|
@ -299,7 +299,7 @@ function customizeHotkeys() {
|
||||||
|
|
||||||
window.onkeydown = event => {
|
window.onkeydown = event => {
|
||||||
if (event.keyCode === 27) {
|
if (event.keyCode === 27) {
|
||||||
top.history.replaceState('', top.document.title, top.location.origin + top.location.pathname);
|
top.dispatchEvent(new CustomEvent('closeOptions'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -593,7 +593,7 @@ Object.assign(handleEvent, {
|
||||||
if (!this.eventHandled) {
|
if (!this.eventHandled) {
|
||||||
this.eventHandled = true;
|
this.eventHandled = true;
|
||||||
this.dataset.href += event.shiftKey || event.button === 2 ?
|
this.dataset.href += event.shiftKey || event.button === 2 ?
|
||||||
'?url=' + encodeURIComponent(tabURL) : '';
|
'?search=' + encodeURIComponent(`url:${tabURL}`) : '';
|
||||||
handleEvent.openURLandHide.call(this, event);
|
handleEvent.openURLandHide.call(this, event);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user