49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
console.log("Initializing Tab Provenance");
|
|
const MENU_ID = "get-opening-tab";
|
|
|
|
browser.runtime.onInstalled.addListener(() => {
|
|
browser.menus.create({
|
|
"id": MENU_ID,
|
|
"title": "What Opened This?",
|
|
"contexts": ["all"]
|
|
});
|
|
});
|
|
|
|
browser.runtime.onStartup.addListener(() => {
|
|
browser.menus.create({
|
|
"id": MENU_ID,
|
|
"title": "What Opened This?",
|
|
"contexts": ["all"]
|
|
});
|
|
});
|
|
|
|
browser.menus.onClicked.addListener(async (info, tab) => {
|
|
if (info.menuItemId === MENU_ID) {
|
|
console.log(`Tab id: ${tab.id}`);
|
|
const target = { "tabId": tab.id };
|
|
try {
|
|
const url = await browser.sessions.getTabValue(tab.id, "provenance");
|
|
const alertMsg = (typeof url === "undefined") ? "No opening url found" : `This tab was opened by: ${url}`;
|
|
|
|
const func = (alertMsg) => {
|
|
alert(alertMsg);
|
|
};
|
|
const args = [alertMsg];
|
|
const injectionResult = await browser.scripting.executeScript({target, args, func});
|
|
} catch (err) {
|
|
await browser.scripting.executeScript({func: () => { alert("Tab not found"); }, target});
|
|
return;
|
|
}
|
|
|
|
}
|
|
});
|
|
// might want to instead use async functions
|
|
|
|
browser.webNavigation.onCreatedNavigationTarget.addListener((details) => {
|
|
browser.tabs.get(details.sourceTabId).then((source) => {
|
|
browser.sessions.setTabValue(details.tabId,
|
|
"provenance",
|
|
source.url) // TODO encode more
|
|
});
|
|
});
|