Initial functionality commit

This commit is contained in:
Greg Shuflin 2023-02-24 00:28:36 -08:00
commit a2e2009ff7
3 changed files with 82 additions and 0 deletions

BIN
icons/48.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

29
manifest.json Normal file
View File

@ -0,0 +1,29 @@
{
"manifest_version": 3,
"name": "Tab Provenance",
"description": "Tells you what site you opened a tab from",
"version": "0.1",
"browser_specific_settings": {
"gecko": {
"id": "tab-provenance@example.com"
}
},
"icons": {
"48": "icons/48.png"
},
"background": {
"scripts": ["tab_provenance.js"]
},
"permissions": [
"tabs",
"activeTab",
"scripting",
"sessions",
"menus",
"webNavigation"
]
}

53
tab_provenance.js Normal file
View File

@ -0,0 +1,53 @@
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");
console.log(`URL: ${url}`);
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});
console.log(injectionResult);
} catch (err) {
console.log(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
});
});