-1

i was trying to create a js function inside a chrome extension that is supposed to have a string as a parameter and replace every letter of that string to a specific char and it is working if you pass the parameter as : 'con("string is here");' , but if pass as a var that contains the string it doesn't work like : con(varThatHasString); and chrome console error: Uncaught TypeError: Cannot read property 'split' of undefined at con ,

my js:

var letters = [
    'q','ض',
    'w','ص',
    'e','ث',
    'r','ق',
    't','ف',
    'y','غ',
    'u','ع',
    'i','ه',
    'o','خ',
    'p','ح',
    'a','ش',
    's','س',
    'd','ي',
    'f','ب',
    'g','ل',
    'h','ا',
    'j','ت',
    'k','ن',
    'l','م',
    'z','ئ',
    'x','ء',
    'c','ؤ',
    'v','ر',
    'b','لا',
    'n','ى',
    'm','ة',
    '[','ج',
    ']','د',
    ';','ك',
    ',','و',
    '.','ز',
    '/','ظ',
    "'",'ط',
    ' ',' ',
    '1','1',
    '2','2',
    '3','3',
    '4','4',
    '5','5',
    '6','6',
    '7','7',
    '8','8',
    '9','9',
    '0','0'
     ];//all the used charchters
var lastSelect;// last selected text from page 

function con(english) {//convert from english to arabic

    var aoutput = new Array;
    var chars = english.split(''); 
    for(var i=0;i < chars.length;i++){
    aoutput.push(letters[letters.indexOf(chars[i])+1]);
 }
    var ar = aoutput.join();
    var res = ar.replace(/,/g,'');
    document.getElementById('copy_box').value = res; 
    return res;

}
//code will run only when there is text already selected on page
chrome.tabs.executeScript( null, {code:"window.getSelection().toString();"}, function(results){ 
lastSelect = results[0];
});
con(last_select);

when i run the extension it just doesn't show anything in the text field ,however when i go to the console and try the function itself it works great and try to con(last_select); it works great and it shows output in text field

White Death
  • 408
  • 5
  • 12
  • The code in your question never calls `con`. Post the calling code for more specific help. – Michael Geary Sep 06 '19 at 23:26
  • Sorry i didn't show the right code i fixed it now – White Death Sep 06 '19 at 23:31
  • 2
    Extension API is asynchronous so you need to use the result **inside** the callback or promisify the API and switch to async/await syntax as shown in the proposed duplicate topic. BTW there's a typo in your code: `last_select` should be `lastSelect`. – wOxxOm Sep 07 '19 at 04:31

1 Answers1

-1

There is no reason that a string variable can't be passed into your con() function. If you were passing in anything other than undefined (even null), it would throw a TypeError. Therefore, the variable you're passing must be incorrectly undefined.

Ensure that whatever variable you pass into there is not undefined and your function will work just fine.

Edit:

Now that you've included your code, @wOxxOm's comment is the real answer; the problem is that chrome.tabs.executeScript is async so you're calling con(last_select) before last_select is set.

mdrichardson
  • 7,141
  • 1
  • 7
  • 21