Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Add a 'translate selection' intent
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Oct 11, 2019
1 parent ae4103a commit 8b2d20d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions extension/background/pageMetadata-contentScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* globals communicate */

this.pageMetadataContentScript = (function() {
communicate.register("getSelection", message => {
const selection = window.getSelection();
return { selection: { text: String(selection) } };
});
})();
18 changes: 18 additions & 0 deletions extension/background/pageMetadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* globals content */

this.pageMetadata = (function() {
const exports = {};

exports.getSelection = async function(tabId) {
await content.lazyInject(
tabId,
"/background/pageMetadata-contentScript.js"
);
const resp = await browser.tabs.sendMessage(tabId, {
type: "getSelection",
});
return resp.selection;
};

return exports;
})();
2 changes: 2 additions & 0 deletions extension/content/communicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
this.communicate = (function() {
const exports = {};
const HANDLERS = {};

exports.register = function(type, handler) {
if (HANDLERS[type]) {
throw new Error(`There is already a handler registerd for ${type}`);
}
HANDLERS[type] = handler;
};

exports.handle = async function(script, message, sender) {
log.messaging(`${script}->`, JSON.stringify(message));
if (!HANDLERS[message.type]) {
Expand Down
23 changes: 22 additions & 1 deletion extension/intents/navigation/navigation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals searching, serviceList */
/* globals searching, serviceList, pageMetadata */

this.intents.navigation = (function() {
this.intentRunner.registerIntent({
Expand Down Expand Up @@ -82,4 +82,25 @@ this.intents.navigation = (function() {
browser.tabs.update(tab.id, { url: translation });
},
});

this.intentRunner.registerIntent({
name: "navigation.translateSelection",
match: `
translate (this |) selection (to english |)
`,
examples: ["Translate selection"],
async run(context) {
const tab = (await browser.tabs.query({ active: true }))[0];
const selection = await pageMetadata.getSelection(tab.id);
if (!selection || !selection.text) {
const e = new Error("No text selected");
e.displayMessage = "No text selected";
throw e;
}
const url = `https://translate.google.com/#view=home&op=translate&sl=auto&tl=en&text=${encodeURIComponent(
selection.text
)}`;
await browser.tabs.create({ url });
},
});
})();
1 change: 1 addition & 0 deletions extension/manifest.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"catcher.js",
"settings.js",
"js/vendor/fuse.js",
"background/pageMetadata.js",
"background/main.js",
"background/voiceSchema.js",
"background/telemetry.js",
Expand Down

0 comments on commit 8b2d20d

Please sign in to comment.