Skip to content

Commit

Permalink
Merge pull request #43 from jtagcat/kbshort
Browse files Browse the repository at this point in the history
Implement keyboard shortcuts (commands).
  • Loading branch information
az0 committed Aug 28, 2021
2 parents 751273e + 76ba411 commit d81197f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 75 deletions.
6 changes: 6 additions & 0 deletions commands/commands.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>Link Gopher</title>
<script type="module" src='commands.js'></script>
</head></html>
13 changes: 13 additions & 0 deletions commands/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/commands

import { handler } from '../popup/js/functions.js';

browser.commands.onCommand.addListener(function (command) {
if (command === "extract-all") {
handler(false);
}

if (command === "extract-some") {
handler(true);
}
});
12 changes: 12 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,17 @@
"default_icon": "icons/link-gopher-32.png",
"default_title": "__MSG_extensionName__",
"default_popup": "popup/popup.html"
},

"background": {
"page": "commands/commands.html"
},
"commands": {
"extract-all": {
"description": "__MSG_extractAll__"
},
"extract-some": {
"description": "__MSG_extractSome__"
}
}
}
74 changes: 74 additions & 0 deletions popup/js/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @function handler
* @param {boolean} filtering
*/
export function handler(filtering = false) {
var tabId;

return getCurrentTab()
.then(items => { tabId = items[0].id; return injectScript(tabId); })
.then(item => {
const url = `${chrome.extension.getURL('browser/linkgopher.html')}?` +
`tabId=${tabId}&filtering=${filtering}`;
return openTab(url);
})
.catch(error => window.alert(error));
};

/**
* Get active tab of current window.
*
* @function getCurrentTab
*/
function getCurrentTab() {
return new Promise((res, rej) => {
const queryInfo = {
active: true,
currentWindow: true
};

chrome.tabs.query(queryInfo, items => passNext(items, res, rej));
});
};

/**
* Create tab with extension's page.
*
* @function openTab
* @param {string} url
*/
export function openTab(url) {
return new Promise((res, rej) => {
const createProperties = {active: true, url};
chrome.tabs.create(createProperties, tab => passNext(tab, res, rej));
});
};

/**
* Inject script into tab
*
* @function injectScript
* @param {number} tabId -- The ID of tab.
* @param {string} file -- Pathname of script
*/
function injectScript(tabId, file = '/content-script.js') {
return new Promise((res, rej) => {
const details = {
file,
runAt: 'document_start'
};

chrome.tabs.executeScript(tabId, details, item => passNext(item, res, rej));
});
};

/**
* @function passNext
* @param {*} result
* @param {function} fulfill
* @param {function} reject
*/
function passNext(result, fulfill, reject) {
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError);
return fulfill(result);
};
78 changes: 3 additions & 75 deletions popup/js/popup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
'use strict';

import { handler, openTab } from './functions.js';

document.getElementById('extract-all').addEventListener('click', event => {
handler(false).then(() => window.close());
});
Expand All @@ -21,78 +24,3 @@ document.getElementById('about-linkgopher').addEventListener('click', event => {
const container = document.getElementById(item.id);
container.innerText = chrome.i18n.getMessage(item.messageId);
})

/**
* @function handler
* @param {boolean} filtering
*/
function handler(filtering = false) {
var tabId;

return getCurrentTab()
.then(items => { tabId = items[0].id; return injectScript(tabId); })
.then(item => {
const url = `${chrome.extension.getURL('browser/linkgopher.html')}?` +
`tabId=${tabId}&filtering=${filtering}`;
return openTab(url);
})
.catch(error => window.alert(error));
};

/**
* Get active tab of current window.
*
* @function getCurrentTab
*/
function getCurrentTab() {
return new Promise((res, rej) => {
const queryInfo = {
active: true,
currentWindow: true
};

chrome.tabs.query(queryInfo, items => passNext(items, res, rej));
});
};

/**
* Create tab with extension's page.
*
* @function openTab
* @param {string} url
*/
function openTab(url) {
return new Promise((res, rej) => {
const createProperties = {active: true, url};
chrome.tabs.create(createProperties, tab => passNext(tab, res, rej));
});
};

/**
* Inject script into tab
*
* @function injectScript
* @param {number} tabId -- The ID of tab.
* @param {string} file -- Pathname of script
*/
function injectScript(tabId, file = '/content-script.js') {
return new Promise((res, rej) => {
const details = {
file,
runAt: 'document_start'
};

chrome.tabs.executeScript(tabId, details, item => passNext(item, res, rej));
});
};

/**
* @function passNext
* @param {*} result
* @param {function} fulfill
* @param {function} reject
*/
function passNext(result, fulfill, reject) {
if (chrome.runtime.lastError) return reject(chrome.runtime.lastError);
return fulfill(result);
};

0 comments on commit d81197f

Please sign in to comment.