WIP: add styles.order

This commit is contained in:
eight04 2021-12-09 19:25:44 +08:00
parent f1cb26d102
commit 253e5d5f34
7 changed files with 52 additions and 4 deletions

View File

@ -734,6 +734,9 @@
"message": "Live reload", "message": "Live reload",
"description": "The label of live-reload feature" "description": "The label of live-reload feature"
}, },
"manageExecutionOrder": {
"message": "Change execution order"
},
"manageFavicons": { "manageFavicons": {
"message": "Favicons in applies-to column", "message": "Favicons in applies-to column",
"description": "Label for the checkbox that toggles applies-to favicons in the new UI on manage page" "description": "Label for the checkbox that toggles applies-to favicons in the new UI on manage page"

View File

@ -60,6 +60,7 @@ const styleMan = (() => {
const DELETE_IF_NULL = ['id', 'customName', 'md5Url', 'originalMd5']; const DELETE_IF_NULL = ['id', 'customName', 'md5Url', 'originalMd5'];
/** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */ /** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */
let ready = init(); let ready = init();
let order = {};
chrome.runtime.onConnect.addListener(handleLivePreview); chrome.runtime.onConnect.addListener(handleLivePreview);
// function handleColorScheme() { // function handleColorScheme() {
@ -71,6 +72,17 @@ const styleMan = (() => {
} }
}); });
prefs.subscribe(['styles.order'], (key, value) => {
order = {};
value.forEach((uid, i) => {
const id = uuidIndex.get(uid);
if (id) {
order[id] = i;
}
});
msg.broadcast({method: 'styleSort', order});
});
//#endregion //#endregion
//#region Exports //#region Exports
@ -149,7 +161,11 @@ const styleMan = (() => {
async getSectionsByUrl(url, id, isInitialApply) { async getSectionsByUrl(url, id, isInitialApply) {
if (ready.then) await ready; if (ready.then) await ready;
if (isInitialApply && prefs.get('disableAll')) { if (isInitialApply && prefs.get('disableAll')) {
return {disableAll: true}; return {
cfg: {
disableAll: true,
},
};
} }
const sender = CHROME && this && this.sender || {}; const sender = CHROME && this && this.sender || {};
if (sender.frameId === 0) { if (sender.frameId === 0) {
@ -171,7 +187,7 @@ const styleMan = (() => {
} }
return id return id
? cache.sections[id] ? {[id]: cache.sections[id]} : {} ? cache.sections[id] ? {[id]: cache.sections[id]} : {}
: cache.sections; : Object.assign({cfg: {order}}, cache.sections);
}, },
/** @returns {Promise<StyleObj>} */ /** @returns {Promise<StyleObj>} */

View File

@ -56,6 +56,9 @@
return NOP; return NOP;
} }
return API.styles.getSectionsByUrl(url, id).then(sections => { return API.styles.getSectionsByUrl(url, id).then(sections => {
if (sections.cfg) {
delete sections.cfg;
}
const tasks = []; const tasks = [];
for (const section of Object.values(sections)) { for (const section of Object.values(sections)) {
const styleId = section.id; const styleId = section.id;

View File

@ -13,11 +13,19 @@
let hasStyles = false; let hasStyles = false;
let isDisabled = false; let isDisabled = false;
let isTab = !chrome.tabs || location.pathname !== '/popup.html'; let isTab = !chrome.tabs || location.pathname !== '/popup.html';
let order = {};
const isFrame = window !== parent; const isFrame = window !== parent;
const isFrameAboutBlank = isFrame && location.href === 'about:blank'; const isFrameAboutBlank = isFrame && location.href === 'about:blank';
const isUnstylable = !chrome.app && document instanceof XMLDocument; const isUnstylable = !chrome.app && document instanceof XMLDocument;
const styleInjector = StyleInjector({ const styleInjector = StyleInjector({
compare: (a, b) => a.id - b.id, compare: (a, b) => {
const ia = order[a.id];
const ib = order[b.id];
if (ia === ib) return 0;
if (ia == null) return 1;
if (ib == null) return -1;
return ia - ib;
},
onUpdate: onInjectorUpdate, onUpdate: onInjectorUpdate,
}); });
// dynamic iframes don't have a URL yet so we'll use their parent's URL (hash isn't inherited) // dynamic iframes don't have a URL yet so we'll use their parent's URL (hash isn't inherited)
@ -100,7 +108,11 @@
parentStyles && await new Promise(onFrameElementInView) && parentStyles || parentStyles && await new Promise(onFrameElementInView) && parentStyles ||
!isFrameAboutBlank && chrome.app && !chrome.tabs && tryCatch(getStylesViaXhr) || !isFrameAboutBlank && chrome.app && !chrome.tabs && tryCatch(getStylesViaXhr) ||
await API.styles.getSectionsByUrl(matchUrl, null, true); await API.styles.getSectionsByUrl(matchUrl, null, true);
isDisabled = styles.disableAll; if (styles.cfg) {
isDisabled = styles.cfg.disableAll;
order = styles.cfg.order || {};
delete styles.cfg;
}
hasStyles = !isDisabled; hasStyles = !isDisabled;
if (hasStyles) { if (hasStyles) {
window[SYM] = styles; window[SYM] = styles;
@ -166,10 +178,18 @@
} }
break; break;
case 'styleSort':
order = request.order;
styleInjector.sort();
break;
case 'urlChanged': case 'urlChanged':
if (!hasStyles && isDisabled || matchUrl === request.url) break; if (!hasStyles && isDisabled || matchUrl === request.url) break;
matchUrl = request.url; matchUrl = request.url;
API.styles.getSectionsByUrl(matchUrl).then(sections => { API.styles.getSectionsByUrl(matchUrl).then(sections => {
if (sections.cfg) {
delete sections.cfg;
}
hasStyles = true; hasStyles = true;
styleInjector.replace(sections); styleInjector.replace(sections);
}); });

View File

@ -78,6 +78,8 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
_addRemoveElements(enable); _addRemoveElements(enable);
if (enable) _toggleObservers(true); if (enable) _toggleObservers(true);
}, },
sort: _sort,
}; };
function _add(style) { function _add(style) {

View File

@ -126,6 +126,8 @@
'popupWidth': 246, // popup width in pixels 'popupWidth': 246, // popup width in pixels
'updateInterval': 24, // user-style automatic update interval, hours (0 = disable) 'updateInterval': 24, // user-style automatic update interval, hours (0 = disable)
'styles.order': [],
}; };
const knownKeys = Object.keys(defaults); const knownKeys = Object.keys(defaults);
/** @type {PrefsValues} */ /** @type {PrefsValues} */

View File

@ -314,6 +314,8 @@
</label> </label>
</div> </div>
<button id="execution-order-button" i18n-text="manageExecutionOrder"></button>
<button id="manage-options-button" i18n-text="openOptions"></button> <button id="manage-options-button" i18n-text="openOptions"></button>
</details> </details>
</div> </div>