mock-violentmonkey
Version:
Mock violentmonkey's globals for testing userscripts
53 lines • 1.42 kB
JavaScript
import { VMStorage } from '../vm-storage.js';
const allTabs = new VMStorage(() => new Set());
const toFullOptions = (options) => {
options ??= {};
options = typeof options === 'boolean' ? { active: !options } : options;
return {
active: options.active ?? true,
container: options.container ?? 0,
insert: options.insert ?? true,
pinned: options.pinned ?? false,
};
};
const openInTab = (url, options) => {
const close = () => {
allTabs.get(true).delete(tab);
returnValue.closed = true;
returnValue.onclose?.();
};
const tab = {
url,
options: toFullOptions(options),
close,
};
allTabs.get(true).add(tab);
const returnValue = {
onclose: null,
closed: false,
close,
};
return returnValue;
};
const getTabs = (url) => {
const tabs = allTabs.get(false);
if (!tabs) {
return [];
}
if (url === undefined) {
return [...tabs];
}
const result = [];
const matchesUrl = (url_) => url instanceof RegExp ? url.test(url_) : url_ === url;
for (const tab of tabs) {
if (matchesUrl(tab.url)) {
result.push(tab);
}
}
return result;
};
export { openInTab as GM_openInTab, getTabs };
Object.defineProperty(globalThis, 'GM_openInTab', {
value: openInTab,
});
//# sourceMappingURL=open-in-tab.js.map