Skip to content

Commit

Permalink
fix for callback value reading
Browse files Browse the repository at this point in the history
since version 50 ff pass an array result into its callback, however this commit keeps compatibility to older versions
  • Loading branch information
renanbr committed Nov 22, 2016
1 parent abc185e commit 3aa46c3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions popup/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ window.onload = function () {
null,

// send to the next funcion the selected text and the website lang
{ code : "[window.getSelection().toString(),document.documentElement.lang]" },
{ code: 'var result = { "text" : window.getSelection().toString(), "lang" : document.documentElement.lang }; result' },

function (data) {
// Keep compatibility to FF version <= 49
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript#Compatibility_notes
if (Object.prototype.toString.call(data) !== '[object Array]') {
data = data[data];
}

var text = data[0].text;
var textLang = data[0].lang;
var userLang = browser.i18n.getUILanguage().substring(0, 2).toUpperCase();
var text = data[0];
if (!text) {
// when no text is selected
// open welcome page
Expand All @@ -23,7 +30,7 @@ window.onload = function () {
} else {
// when a text is selected
// build URL with translation result
var textLang = data[1].substring(0, 2).toUpperCase();
var textLang = textLang.substring(0, 2).toUpperCase();
var url = pairUrl[textLang + "-" + userLang];
if (!url) {
// @todo give a feedback
Expand Down

0 comments on commit 3aa46c3

Please sign in to comment.