Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement keyboard shortcuts (commands). #43

Merged
merged 2 commits into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 === "extarct-all") {
jtagcat marked this conversation as resolved.
Show resolved Hide resolved
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": {
"extarct-all": {
jtagcat marked this conversation as resolved.
Show resolved Hide resolved
"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);
};