commit a2e2009ff746670036f48643129ce6fb1e5cc4b7 Author: Greg Shuflin Date: Fri Feb 24 00:28:36 2023 -0800 Initial functionality commit diff --git a/icons/48.png b/icons/48.png new file mode 100644 index 0000000..01fdf18 Binary files /dev/null and b/icons/48.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..2aae6c5 --- /dev/null +++ b/manifest.json @@ -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" + ] +} diff --git a/tab_provenance.js b/tab_provenance.js new file mode 100644 index 0000000..638f115 --- /dev/null +++ b/tab_provenance.js @@ -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 + }); +});