-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from jtagcat/kbshort
Implement keyboard shortcuts (commands).
- Loading branch information
Showing
5 changed files
with
108 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters