/* add a context menu for setting the background */ browser.menus.create({ id: "set-background", title: "Set Background Image", contexts: [ "image" ], icons: { "16": "icons/background.png", "32": "icons/background.png", "64": "icons/background.png", "128": "icons/background.png", "256": "icons/background.png" } }) /* add a context menu for accessing repo on github */ browser.menus.create({ id: "search-github", title: "Open in Github", contexts: [ "selection" ] }) browser.menus.onClicked.addListener(info => { if (info.menuItemId == "set-background") { browser.runtime.sendNativeMessage("bg_app", info.srcUrl); } else if (info.menuItemId == "search-github" && info.selectionText) { const trimmedText = info.selectionText.trim(); const githubUrl = `https://github.com/${trimmedText}`; chrome.tabs.create({ url: githubUrl }) } }); /* add functionality to commands defined in manifest.json */ chrome.commands.onCommand.addListener(function(action) { action.preventDefault(); switch (action) { case "toggle-pin-tab": /* toggle current tab pinning */ chrome.tabs.query({ currentWindow: true, active: true }, function(found_tab) { const currentTab = found_tab[0] const toggledValue = !currentTab.pinned; chrome.tabs.update(currentTab.id, { pinned: toggledValue }); }); break; /* switch between tabs */ case "tab-up": chrome.tabs.query({ currentWindow: true, active: true }, function(found_tab) { switch_tab(found_tab[0].index - 1); }); break; case "tab-down": chrome.tabs.query({ currentWindow: true, active: true }, function(found_tab) { switch_tab(found_tab[0].index + 1); }); break; /* close tab */ case "tab-close": chrome.tabs.query({ currentWindow: true, active: true }, function(found_tab) { browser.tabs.remove(found_tab[0].id); }); break; case "scroll-half-down": window.scrollBy(0, window.innerHeight / 2, "smooth"); console.log(window) console.log("hello world") break; case "scroll-half-up": window.scrollBy(0, -(window.innerHeight / 2), "smooth"); console.log(window) console.log("hello world") break; } }); function switch_tab(tab_number) { /* I don't want it to cycle around */ if (tab_number < 0) { return; } chrome.tabs.query({ currentWindow: true, active: true }, function(found_tabs) { const currentTab = found_tabs[0]; if (currentTab.index == tab_number) { /* we're already on the current tab */ return; } /* change the selected tab to active */ browser.tabs.query({ currentWindow: true }, function(window_tabs) { if (typeof window_tabs.at(tab_number) != "undefined") { chrome.tabs.update(window_tabs.at(tab_number).id, { active: true }); } }); }); }