Skip to content

Instantly share code, notes, and snippets.

@chrisdone-artificial
Last active October 10, 2025 13:11
Show Gist options
  • Save chrisdone-artificial/b4505825a5f79fd3141869df052ec313 to your computer and use it in GitHub Desktop.
Save chrisdone-artificial/b4505825a5f79fd3141869df052ec313 to your computer and use it in GitHub Desktop.
temp-link-opener
// Create the context menu item when the extension is installed
browser.contextMenus.create({
id: "open-link-temporarily",
title: "Open Link temporarily",
contexts: ["link"]
});
// Track tabs that should be auto-closed
const tempTabs = new Map();
// Handle context menu clicks
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "open-link-temporarily" && info.linkUrl) {
// Open the link in a new background tab
browser.tabs.create({
url: info.linkUrl,
active: false // Don't focus the new tab
}).then((newTab) => {
// Schedule the tab to close after 2 seconds
const timeoutId = setTimeout(() => {
browser.tabs.remove(newTab.id).catch(() => {
// Ignore errors if tab was already closed
});
tempTabs.delete(newTab.id);
}, 2000);
// Store the timeout ID in case we need to cancel it
tempTabs.set(newTab.id, timeoutId);
});
}
});
// Clean up if the user manually closes the tab before the timeout
browser.tabs.onRemoved.addListener((tabId) => {
if (tempTabs.has(tabId)) {
clearTimeout(tempTabs.get(tabId));
tempTabs.delete(tabId);
}
});
{
"manifest_version": 2,
"name": "Temporary Link Opener",
"version": "1.0",
"description": "Open links temporarily in background tabs that auto-close after 2 seconds",
"permissions": [
"contextMenus",
"tabs"
],
"background": {
"scripts": ["background.js"]
},
"icons": {
"48": "icon.png"
},
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"data_collection_permissions": {
"required": ["none"]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment