Skip to content

Commit

Permalink
add document/target URL filters
Browse files Browse the repository at this point in the history
  • Loading branch information
waldner committed Mar 3, 2022
1 parent 296ace7 commit ae5d44e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ For each configuration line specify:
- The menu text for the entry (will be shown when you right-click)
- The NM host to use. Use **`runwith`** unless you know what you're doing.
- The context in which you want the menu entry to appear: `link`, `selection`, `image`, `editable`, `page`. `page` applies when none of the more-specific ones does.
- (Optional) The [documentUrlPatterns](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns), comma separated, where you want the menu to be shown; this refers to the document URL, ie the URL you see in the browser's address bar. By default, all URLs are enabled; in match pattern syntax, this means the value is `<all_urls>`.
- (Optional) The [targetUrlPatterns](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns), comma separated, for which you want the menu to be shown; this applies only to the `link` and `image` contexts, and refers to the link target or image source respectively. It is ignored for other contexts. By default, all URLs are enabled; in match pattern syntax, this means the value is `<all_urls>`.
- The actual command you want to run. **Separate words using commas**. Use the following special words to indicate the current link, selected text or image URL respectively: **`%%LINK%%`**, **`%%SELECTION%%`**, **`%%IMAGE%%`** (only the one appropriate for the context). At runtime, the **`%%WORD%%`** will be replaced with the actual link, selection or image URL value. Additionally, the **`%%TAB-URL%%`** and **`TAB-TITLE`** keywords are available in all contexts, and contain, as their name implies, the current tab's URL and title.
- Whether to run the command through a shell. This is normally needed only if you have special shell characters in the command (redirections, pipes, etc), and shouldn't be normally required.
- Whether you want the NM host program to wait for the command to finish or not. Unless you want to run graphical or detached commands, you should check this field. If the context is `editable`, this valued is ignored, as we must always wait for the command to complete to capture the output to send to the editable.
Expand Down
16 changes: 14 additions & 2 deletions addon/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,24 @@ function actionFunc(action, nmhost, context, shell, wait, info, tab){
}

function createMenuItem(item){
var id = browser.menus.create({

var menuProps = {
id: item.id,
title: item.title,
contexts: item.contexts,
});
}

if (item.documentUrlPatterns) {
menuProps.documentUrlPatterns = item.documentUrlPatterns;
}

if (item.contexts[0] == "link" || item.contexts[0] == "image") {
if (item.targetUrlPatterns) {
menuProps.targetUrlPatterns = item.targetUrlPatterns;
}
}

var id = browser.menus.create(menuProps);
return id;
}

Expand Down
2 changes: 1 addition & 1 deletion addon/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "RunWith",
"description": "Run external commands on contextual elements",
"version": "0.15",
"version": "0.16",
"applications": {
"gecko": {
"id": "[email protected]",
Expand Down
85 changes: 72 additions & 13 deletions addon/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function createTableRow(data){
input.style.width = '100%';
if (data) {
input.value = data.nmhost;
if (input.value == null || input.value == "") {
input.value = "runwith";
}
}
input.placeholder = 'NM Host';
input.required = true;
Expand Down Expand Up @@ -48,6 +51,37 @@ function createTableRow(data){
}
tr.insertCell().appendChild(select);

var input = document.createElement('input');
input.type = 'text';
input.name = 'document_urls';
input.style.width = '100%';
if (data) {
if (data.documentUrlPatterns == null || data.documentUrlPatterns == "") {
input.value = "<all_urls>";
} else {
input.value = data.documentUrlPatterns;
}
}
input.placeholder = 'Document URL patterns (optional)';
input.required = false;
tr.insertCell().appendChild(input);

var input = document.createElement('input');
input.type = 'text';
input.name = 'target_urls';
input.style.width = '100%';
if (data) {
input.value = data.targetUrlPatterns;
if (data.targetUrlPatterns == null || data.targetUrlPatterns == "") {
input.value = "<all_urls>";
} else {
input.value = data.targetUrlPatterns;
}
}
input.placeholder = 'Target URL patterns (optional)';
input.required = false;
tr.insertCell().appendChild(input);

var input = document.createElement('input');
input.type = 'text';
input.name = 'command';
Expand Down Expand Up @@ -79,7 +113,7 @@ function createTableRow(data){
}
tr.insertCell().appendChild(input);

var deleteButton = createButton("Delete", "deleteButton", function(){
var deleteButton = createButton("Del", "deleteButton", function(){
deleteRow(tr)
}, false);

Expand All @@ -100,7 +134,7 @@ async function populateDOM(config, overwrite) {
console.log('conf is: ' + JSON.stringify(conf));

var mainTable = document.getElementById('mainTable');
mainTable.style.width = "90%";
mainTable.style.width = "200%";

var mainTableHead = document.getElementById('mainTableHead');
var mainTableBody = document.getElementById('mainTableBody');
Expand All @@ -119,42 +153,52 @@ async function populateDOM(config, overwrite) {
var tr = mainTableHead.insertRow();

tr.style.fontWeight = 'bold';
tr.style.fontSize = '120%';
tr.style.fontSize = '100%';

var cell = tr.insertCell();
cell.style.width = "15%";
cell.align = "center";
cell.appendChild(document.createTextNode('Menu title'));

var cell = tr.insertCell();
cell.style.width = "10%";
cell.style.width = "7%";
cell.align = "center";
cell.appendChild(document.createTextNode('NM host'));

var cell = tr.insertCell();
cell.style.width = "13%";
cell.style.width = "9%";
cell.align = "center";
cell.appendChild(document.createTextNode('Context'));

var cell = tr.insertCell();
cell.style.width = "45%";
cell.style.width = "18%";
cell.align = "center";
cell.appendChild(document.createTextNode('Document URLs'));

var cell = tr.insertCell();
cell.style.width = "18%";
cell.align = "center";
cell.appendChild(document.createTextNode('Target URLs'));

var cell = tr.insertCell();
cell.style.width = "40%";
cell.align = "center";
cell.appendChild(document.createTextNode('Command'));

var cell = tr.insertCell();
cell.style.width = "5%";
cell.style.width = "3%";
cell.align = "center";
cell.appendChild(document.createTextNode('Shell'));

var cell = tr.insertCell();
cell.style.width = "5%";
cell.style.width = "3%";
cell.align = "center";
cell.appendChild(document.createTextNode('Wait'));

var cell = tr.insertCell();
cell.style.width = "7%";
cell.style.width = "3%";
cell.align = "center";
cell.appendChild(document.createTextNode('Delete'));
cell.appendChild(document.createTextNode('Del'));

for (var i = 0; i < conf.conf.length; i++) {
var tr = createTableRow(conf.conf[i]);
Expand Down Expand Up @@ -215,6 +259,7 @@ async function saveOrExportConfig(e){
notify('Configuration saved');
}

populateDOM(config, true);
// You must return false to prevent the default form behavior
return false;
}
Expand Down Expand Up @@ -254,14 +299,28 @@ function collectConfig(){
}
elem.contexts = contexts;

// documentUrlPatterns
if (mainTableBody.rows[row].cells[3].childNodes[0].value == "") {
elem.documentUrlPatterns = ["<all_urls>"];
} else {
elem.documentUrlPatterns = mainTableBody.rows[row].cells[3].childNodes[0].value.split(/ *, */);
}

// targetUrlPatterns
if (mainTableBody.rows[row].cells[4].childNodes[0].value == "") {
elem.targetUrlPatterns = ["<all_urls>"];
} else {
elem.targetUrlPatterns = mainTableBody.rows[row].cells[4].childNodes[0].value.split(/ *, */);
}

// command
elem.action = mainTableBody.rows[row].cells[3].childNodes[0].value.split(/ *, */);
elem.action = mainTableBody.rows[row].cells[5].childNodes[0].value.split(/ *, */);

// shell
elem.shell = mainTableBody.rows[row].cells[4].childNodes[0].checked;
elem.shell = mainTableBody.rows[row].cells[6].childNodes[0].checked;

// wait
elem.wait = mainTableBody.rows[row].cells[5].childNodes[0].checked;
elem.wait = mainTableBody.rows[row].cells[7].childNodes[0].checked;

config.config.conf.push(elem);
}
Expand Down

0 comments on commit ae5d44e

Please sign in to comment.