Permalink
Browse files

Translate Now-webext 1.2.0:

Allow text selection > 150 characters (workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1338898)
Allow speak actions up to 195 characters
Use new options save/restore mechanism
Fix build script
  • Loading branch information...
1 parent 6d50435 commit afcf2351d31d70f0b267127512ccb2260cfdfcd0 Smile4ever committed Feb 13, 2017
@@ -1,6 +1,7 @@
/// Static variables
var selectedText = "";
var lastTabId = -1;
+var globalAction = "";
//the addons icon is a modified version of http://www.flaticon.com/free-icon/translator-tool_69101
//see their website for licensing information
@@ -42,11 +43,16 @@ function init(){
}
init();
+///Messages
+// listen for messages from the content or options script
browser.runtime.onMessage.addListener(function(message) {
switch (message.action) {
case "refresh-options":
init();
break;
+ case "setSelection":
+ setSelection(message.data);
+ break;
case "notify":
notify(message.data);
break;
@@ -55,6 +61,20 @@ browser.runtime.onMessage.addListener(function(message) {
}
});
+// See also https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/Tabs/sendMessage
+function sendMessage(action, data){
+ function logTabs(tabs) {
+ for (tab of tabs) {
+ // tab.url requires the tabs permission
+ //console.log(tab.url);
+ browser.tabs.sendMessage(tab.id, {"action": action, "data": data});
+ }
+ }
+
+ var querying = browser.tabs.query({currentWindow: true, active: true});
+ querying.then(logTabs, onError);
+}
+
/// Context menus
function initContextMenus(){
@@ -146,15 +166,24 @@ function openTabInner(url){
}
function doClick(selectionText, action){
+ // Ideally, we want to do this
+ // doAction(selectionText, action);
+
+ // But we now do this to circumvent https://bugzilla.mozilla.org/show_bug.cgi?id=1338898
+ globalAction = action;
+ sendMessage("getSelection");
+}
+
+function doAction(selectionText, action){
if(selectionText != "" || selectionText == null){
selectedText = selectionText;
//console.log("selectionText length is " + selectionText.length);
}
if(selectedText == null){
notify("Try another selection");
}else{
- if(selectedText.length > 150 && action == "speak"){
- notify("Selected text is too long. Maximum length is 150.");
+ if(selectedText.length > 195 && action == "speak"){
+ notify("Selected text is too long. Maximum length is 195.");
}else{
var newText = selectedText;
newText = encodeURIComponent(newText);
@@ -169,6 +198,10 @@ function doClick(selectionText, action){
}
}
+function setSelection(selectionText){
+ doAction(selectionText, globalAction);
+}
+
/// Helper functions
function onError(error) {
console.log(`Error: ${error}`);
@@ -1,2 +1,2 @@
#!/bin/bash
-web-ext build --ignore-files webext-build.sh images README.md
+web-ext build --ignore-files build.sh images README.md
@@ -3,13 +3,20 @@
"description": "Right click a selection to translate words in Firefox.",
"homepage_url": "http://hugsmile.eu",
"manifest_version": 2,
- "version": "1.1.1",
+ "version": "1.2.0",
"background": {
"scripts": ["background.js"]
},
"icons": {
"64": "icons/translatenow-64.png"
},
+ "content_scripts": [
+ {
+ "matches": ["<all_urls>"],
+ "js": ["translatenow.js"],
+ "run_at": "document_start"
+ }
+ ],
"applications": {
"gecko": {
"id": "@translatenow",
@@ -1,4 +1,4 @@
-/*const PREFS = {
+const PREFS = {
"translate_now_destination_language": {
"type": "value",
"default": "en"
@@ -15,79 +15,23 @@
"type": "checked",
"default": false
}
-};*/
+};
-function saveOptions() {
- browser.runtime.sendMessage({action: "notify", data: "Saved preferences"});
-
- browser.storage.local.set({
- translate_now_destination_language: document.querySelector("#translate_now_destination_language").value,
- translate_now_source_language: document.querySelector("#translate_now_source_language").value,
- translate_now_reuse_tab: document.querySelector("#translate_now_reuse_tab").checked,
- translate_now_enable_speak: document.querySelector("#translate_now_enable_speak").checked
- }).then(() => browser.runtime.sendMessage({action: "refresh-options"}));
-}
-
-/*function saveOptions() {
+function saveOptions() {
browser.runtime.sendMessage({action: "notify", data: "Saved preferences"});
const values = {};
- for(const p in PREFS) {
+ for(let p in PREFS) {
values[p] = document.getElementById(p)[PREFS[p].type];
}
browser.storage.local.set(values).then(() => browser.runtime.sendMessage({action: "refresh-options"}));
-}*/
-
-function restoreOptions() {
-
- function setDestinationLanguage(result) {
- document.querySelector("#translate_now_destination_language").value = value(result, "en");
- }
-
- function setSourceLanguage(result) {
- document.querySelector("#translate_now_source_language").value = value(result, "auto");
- }
-
- function setReuseTab(result){
- document.querySelector("#translate_now_reuse_tab").checked = value(result, true);
- }
-
- function setEnableSpeak(result){
- document.querySelector("#translate_now_enable_speak").checked = value(result, false);
- }
-
- function onError(error) {
- console.log(`Error: ${error}`);
- }
-
- function value(result,defaultValue){
- for(var key in result) {
- if(result.hasOwnProperty(key)) {
- return result[key];
- }
- }
- return defaultValue;
- }
-
- var getting1 = browser.storage.local.get("translate_now_destination_language");
- getting1.then(setDestinationLanguage, onError);
-
- var getting2 = browser.storage.local.get("translate_now_source_language");
- getting2.then(setSourceLanguage, onError);
-
- var getting3 = browser.storage.local.get("translate_now_reuse_tab");
- getting3.then(setReuseTab, onError);
-
- var getting4 = browser.storage.local.get("translate_now_enable_speak");
- getting4.then(setEnableSpeak, onError);
}
-
-/*function restoreOptions() {
+function restoreOptions() {
browser.storage.local.get(Object.keys(PREFS)).then((result) => {
let val;
- for(const p in PREFS) {
+ for(let p in PREFS) {
if(p in result) {
val = result[p];
}
@@ -97,7 +41,7 @@ function restoreOptions() {
document.getElementById(p)[PREFS[p].type] = val;
}
}).catch(console.error);
-}*/
+}
function init(){
restoreOptions();
Binary file not shown.

0 comments on commit afcf235

Please sign in to comment.