2021-01-01 14:27:58 +00:00
|
|
|
/* global API */// msg.js
|
2021-02-22 12:12:19 +00:00
|
|
|
/* global URLS waitForTabUrl */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const ABOUT_BLANK = 'about:blank';
|
|
|
|
/* exported preinit */
|
|
|
|
const preinit = (async () => {
|
|
|
|
let [tab] = await browser.tabs.query({currentWindow: true, active: true});
|
|
|
|
if (!chrome.app && tab.status === 'loading' && tab.url === ABOUT_BLANK) {
|
2021-02-22 12:12:19 +00:00
|
|
|
tab = await waitForTabUrl(tab);
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
const frames = sortTabFrames(await browser.webNavigation.getAllFrames({tabId: tab.id}));
|
|
|
|
let url = tab.pendingUrl || tab.url || ''; // new Chrome uses pendingUrl while connecting
|
|
|
|
if (url === 'chrome://newtab/' && !URLS.chromeProtectsNTP) {
|
|
|
|
url = frames[0].url || '';
|
|
|
|
}
|
|
|
|
if (!URLS.supported(url)) {
|
|
|
|
url = '';
|
|
|
|
frames.length = 1;
|
|
|
|
}
|
|
|
|
frames[0].url = url;
|
|
|
|
const uniqFrames = frames.filter(f => f.url && !f.isDupe);
|
|
|
|
const styles = await Promise.all(uniqFrames.map(async ({url}) => ({
|
|
|
|
url,
|
|
|
|
styles: await getStyleDataMerged(url),
|
|
|
|
})));
|
|
|
|
return {frames, styles, url};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/* Merges the extra props from API into style data.
|
|
|
|
* When `id` is specified returns a single object otherwise an array */
|
|
|
|
async function getStyleDataMerged(url, id) {
|
|
|
|
const styles = (await API.styles.getByUrl(url, id))
|
|
|
|
.map(r => Object.assign(r.style, r));
|
|
|
|
return id ? styles[0] : styles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @param {browser.webNavigation._GetAllFramesReturnDetails[]} frames */
|
|
|
|
function sortTabFrames(frames) {
|
|
|
|
const unknown = new Map(frames.map(f => [f.frameId, f]));
|
|
|
|
const known = new Map([[0, unknown.get(0) || {frameId: 0, url: ''}]]);
|
|
|
|
unknown.delete(0);
|
|
|
|
let lastSize = 0;
|
|
|
|
while (unknown.size !== lastSize) {
|
|
|
|
for (const [frameId, f] of unknown) {
|
|
|
|
if (known.has(f.parentFrameId)) {
|
|
|
|
unknown.delete(frameId);
|
|
|
|
if (!f.errorOccurred) known.set(frameId, f);
|
|
|
|
if (f.url === ABOUT_BLANK) f.url = known.get(f.parentFrameId).url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastSize = unknown.size; // guard against an infinite loop due to a weird frame structure
|
|
|
|
}
|
|
|
|
const sortedFrames = [...known.values(), ...unknown.values()];
|
|
|
|
const urls = new Set([ABOUT_BLANK]);
|
|
|
|
for (const f of sortedFrames) {
|
|
|
|
if (!f.url) f.url = '';
|
|
|
|
f.isDupe = urls.has(f.url);
|
|
|
|
urls.add(f.url);
|
|
|
|
}
|
|
|
|
return sortedFrames;
|
|
|
|
}
|