2020-11-14 13:12:49 +00:00
|
|
|
import {browser, Menus} from 'webextension-polyfill-ts';
|
2020-11-11 17:17:37 +00:00
|
|
|
import {
|
|
|
|
error,
|
|
|
|
getManifest,
|
|
|
|
getNextQItem,
|
|
|
|
getSettings,
|
|
|
|
newQItemID,
|
|
|
|
QItem,
|
|
|
|
QMessage,
|
|
|
|
removeQItem,
|
2020-11-12 21:42:21 +00:00
|
|
|
saveSettings,
|
|
|
|
updateBadge
|
2020-11-11 17:17:37 +00:00
|
|
|
} from '.';
|
|
|
|
|
|
|
|
let timeoutID: number | null = null;
|
|
|
|
|
|
|
|
browser.browserAction.onClicked.addListener(async () => {
|
|
|
|
// When the extension icon is initially clicked, create a timeout for 500ms
|
|
|
|
// that will open the next queue item when it expires.
|
|
|
|
// If the icon is clicked again in those 500ms, open the options page instead.
|
|
|
|
if (timeoutID === null) {
|
|
|
|
timeoutID = window.setTimeout(async () => {
|
|
|
|
timeoutID = null;
|
|
|
|
const nextItem = await getNextQItem();
|
|
|
|
|
|
|
|
if (nextItem === undefined) {
|
|
|
|
await openOptionsPage();
|
|
|
|
} else {
|
|
|
|
const tabs = await browser.tabs.query({
|
|
|
|
currentWindow: true,
|
|
|
|
active: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const message: QMessage<QItem> = {
|
|
|
|
action: 'queue open url',
|
|
|
|
data: nextItem
|
|
|
|
};
|
|
|
|
|
2020-11-16 21:17:56 +00:00
|
|
|
try {
|
|
|
|
await browser.tabs.sendMessage(tabs[0].id!, message);
|
|
|
|
} catch {
|
|
|
|
await browser.tabs.create({active: true, url: nextItem.url});
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:17:37 +00:00
|
|
|
await removeQItem(nextItem.id);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
} else {
|
|
|
|
window.clearTimeout(timeoutID);
|
|
|
|
timeoutID = null;
|
|
|
|
await openOptionsPage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-12 21:42:21 +00:00
|
|
|
browser.runtime.onMessage.addListener(async (request: QMessage<unknown>) => {
|
|
|
|
if (request.action === 'queue update badge') {
|
|
|
|
await updateBadge();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-11 17:17:37 +00:00
|
|
|
browser.runtime.onInstalled.addListener(async () => {
|
|
|
|
const manifest = getManifest();
|
|
|
|
const settings = await getSettings();
|
|
|
|
|
|
|
|
// Open the options page when:
|
|
|
|
// * The extension is first installed or is updated.
|
|
|
|
// * In development, for convenience.
|
|
|
|
if (
|
|
|
|
manifest.version !== settings.latestVersion ||
|
|
|
|
manifest.nodeEnv === 'development'
|
|
|
|
) {
|
|
|
|
settings.latestVersion = manifest.version;
|
2020-11-14 17:24:20 +00:00
|
|
|
settings.versionGotUpdated = true;
|
2020-11-11 17:17:37 +00:00
|
|
|
await saveSettings(settings);
|
|
|
|
await openOptionsPage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async function openOptionsPage() {
|
|
|
|
return browser.runtime.openOptionsPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The callback function for custom context menu entries.
|
|
|
|
*/
|
|
|
|
function contextCreated() {
|
|
|
|
if (
|
|
|
|
browser.runtime.lastError !== null &&
|
|
|
|
browser.runtime.lastError !== undefined
|
|
|
|
) {
|
|
|
|
error(browser.runtime.lastError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 13:12:49 +00:00
|
|
|
const contextMenus: Menus.CreateCreatePropertiesType[] = [
|
2020-11-11 17:17:37 +00:00
|
|
|
{
|
|
|
|
id: 'queue-add-new-link',
|
|
|
|
title: 'Add to Queue',
|
|
|
|
contexts: ['link']
|
|
|
|
},
|
2020-11-14 13:12:49 +00:00
|
|
|
{
|
|
|
|
id: 'queue-add-new-link-tab',
|
|
|
|
title: 'Add to Queue',
|
|
|
|
contexts: ['tab']
|
2020-11-16 21:28:31 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'queue-open-next-link-in-new-tab',
|
|
|
|
title: 'Open next link in new tab',
|
|
|
|
contexts: ['browser_action']
|
2020-11-14 13:12:49 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2020-11-16 21:28:31 +00:00
|
|
|
const contextMenuIDs: Set<string> = new Set(
|
|
|
|
contextMenus.map((value) => value.id!)
|
|
|
|
);
|
|
|
|
|
2020-11-14 13:12:49 +00:00
|
|
|
for (const menu of contextMenus) {
|
|
|
|
browser.contextMenus.create(menu, contextCreated);
|
|
|
|
}
|
2020-11-11 17:17:37 +00:00
|
|
|
|
|
|
|
browser.contextMenus.onClicked.addListener(async (info, _tab) => {
|
2020-11-16 21:28:31 +00:00
|
|
|
const id = info.menuItemId.toString();
|
|
|
|
if (!contextMenuIDs.has(id)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-11 17:17:37 +00:00
|
|
|
|
2020-11-16 21:28:31 +00:00
|
|
|
const settings = await getSettings();
|
|
|
|
|
|
|
|
if (id.includes('queue-add-new-link')) {
|
2020-11-14 13:12:49 +00:00
|
|
|
let url: string;
|
|
|
|
if (info.menuItemId === 'queue-add-new-link') {
|
|
|
|
url = info.linkUrl!;
|
|
|
|
} else if (info.menuItemId === 'queue-add-new-link-tab') {
|
|
|
|
url = info.pageUrl!;
|
|
|
|
} else {
|
|
|
|
error(`Unknown menuItemId: ${info.menuItemId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:17:37 +00:00
|
|
|
settings.queue.push({
|
|
|
|
added: new Date(),
|
|
|
|
id: newQItemID(settings.queue),
|
2020-11-14 13:12:49 +00:00
|
|
|
text: info.linkText ?? url,
|
|
|
|
url
|
2020-11-11 17:17:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await saveSettings(settings);
|
2020-11-12 21:42:21 +00:00
|
|
|
await updateBadge(settings);
|
2020-11-16 21:28:31 +00:00
|
|
|
} else if (id === 'queue-open-next-link-in-new-tab') {
|
|
|
|
const nextItem = await getNextQItem(settings);
|
|
|
|
if (nextItem === undefined) {
|
|
|
|
await openOptionsPage();
|
|
|
|
} else {
|
|
|
|
await browser.tabs.create({active: true, url: nextItem.url});
|
|
|
|
await removeQItem(nextItem.id);
|
|
|
|
}
|
2020-11-11 17:17:37 +00:00
|
|
|
}
|
|
|
|
});
|