Show a context-menu item to run a page’s command
This commit is contained in:
parent
a1578a12ef
commit
06a08de31a
|
@ -1,8 +1,7 @@
|
||||||
let port = undefined;
|
let port = undefined;
|
||||||
|
|
||||||
|
|
||||||
// Run the shellfox helper program.
|
// Run the shellfox helper program.
|
||||||
function init_shellfox_port() {
|
function initShellfoxProgram() {
|
||||||
port = browser.runtime.connectNative("shellfox");
|
port = browser.runtime.connectNative("shellfox");
|
||||||
port.onDisconnect.addListener((port) => {
|
port.onDisconnect.addListener((port) => {
|
||||||
console.log(port.error);
|
console.log(port.error);
|
||||||
|
@ -38,7 +37,7 @@ function getUrlCommand(url) {
|
||||||
function runUrlCommand(url) {
|
function runUrlCommand(url) {
|
||||||
let command = getUrlCommand(url);
|
let command = getUrlCommand(url);
|
||||||
if (!port)
|
if (!port)
|
||||||
init_shellfox_port();
|
initShellfoxProgram();
|
||||||
if (command && port) {
|
if (command && port) {
|
||||||
port.postMessage(command);
|
port.postMessage(command);
|
||||||
}
|
}
|
||||||
|
@ -57,6 +56,24 @@ function compareRegexComplexity(a, b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Add a context-menu item for running the current page’s associated command.
|
||||||
|
function createContextMenuItem() {
|
||||||
|
browser.menus.create(
|
||||||
|
{
|
||||||
|
id: "run-page-command",
|
||||||
|
title: "Run shell command",
|
||||||
|
contexts: ["page"]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Simply remove the context-menu item.
|
||||||
|
function removeContextMenuItem() {
|
||||||
|
browser.menus.remove("run-page-command");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// When the address-bar button is clicked, run the according command (if any).
|
// When the address-bar button is clicked, run the according command (if any).
|
||||||
browser.pageAction.onClicked.addListener((tab) => {
|
browser.pageAction.onClicked.addListener((tab) => {
|
||||||
runUrlCommand(tab.url);
|
runUrlCommand(tab.url);
|
||||||
|
@ -66,18 +83,36 @@ browser.pageAction.onClicked.addListener((tab) => {
|
||||||
// When a tab’s URL has been changed, enable/disable the address-bar button
|
// When a tab’s URL has been changed, enable/disable the address-bar button
|
||||||
// based on whether or not there is an according command.
|
// based on whether or not there is an according command.
|
||||||
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||||||
if (getUrlCommand(tab.url))
|
let command = getUrlCommand(tab.url);
|
||||||
|
if (command) {
|
||||||
browser.pageAction.show(tabId);
|
browser.pageAction.show(tabId);
|
||||||
else
|
if (tab.active)
|
||||||
|
createContextMenuItem();
|
||||||
|
} else {
|
||||||
browser.pageAction.hide(tabId);
|
browser.pageAction.hide(tabId);
|
||||||
|
if (tab.active)
|
||||||
|
removeContextMenuItem();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// When the active tab has changed, enable/disable the address-bar button based
|
// When the active tab has changed, enable/disable the address-bar button based
|
||||||
// on whether or not there is an according command for it.
|
// on whether or not there is an according command for it.
|
||||||
browser.tabs.onActivated.addListener((activeInfo) => {
|
browser.tabs.onActivated.addListener((activeInfo) => {
|
||||||
|
console.log("ACTIVE");
|
||||||
browser.tabs.get(activeInfo.tabId).then((tab) => {
|
browser.tabs.get(activeInfo.tabId).then((tab) => {
|
||||||
if (getUrlCommand(tab.url))
|
if (getUrlCommand(tab.url)) {
|
||||||
browser.pageAction.show(tab.id);
|
browser.pageAction.show(tab.id);
|
||||||
|
createContextMenuItem();
|
||||||
|
} else {
|
||||||
|
browser.pageAction.hide(tab.id);
|
||||||
|
removeContextMenuItem();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
browser.menus.onClicked.addListener((info, tab) => {
|
||||||
|
if (info.menuItemId == "run-page-command")
|
||||||
|
runUrlCommand(tab.url);
|
||||||
|
});
|
||||||
|
|
Ŝarĝante…
Reference in New Issue