1

I want to pass a variable as text or somehow to make above function to work:

var title = "Hello World";
chrome.tabs.executeScript(tabId, {code: "var param1='"+title+"'; var param2='value2'; "}, function(){ /*some code*/ });

When I execute above function it not pass title to param1, it must define param1 as title. Because title is dynamic, it can be diffrent, so I really need to know how can pass my title variable.

If I change code like this :

chrome.tabs.executeScript(tabId, {code: "var param1='Hello World!'; var param2='value2'; "}, function(){ /*some code*/ });

than it works perfect

Jigberto
  • 1,513
  • 7
  • 27
  • 50
  • What is the value of `tabId`? Also, in case you didn't know: [`chrome.tabs.executeScript`](http://code.google.com/chrome/extensions/tabs.html#method-executeScript) will **not** work in Content scripts. – Rob W Mar 15 '12 at 17:58
  • i am not sure that i understand the question, can you give an example of what output you desire from this? – jbabey Mar 15 '12 at 17:59
  • Above code works good... but param1 value is undefined.. because it not pass. – Jigberto Mar 15 '12 at 18:01
  • @user It does not pass, so it does not work. Add `alert(location.href)` in the code, to verify that the code is correctly injected, in the right tab. – Rob W Mar 15 '12 at 18:50
  • it works...I checked! but it pass param1 as undefenited. IF I change code like this than it work perfect as I want: chrome.tabs.executeScript(tabId, {code: "var param1='Hello World'; var param2='value2'; "}, function(){ /*some code*/ }); – Jigberto Mar 15 '12 at 20:27
  • @user1267305 That is impossible. Does `param1` **really** contain "Hello World", or does it also contain something else? Show your exact code, because I'm pretty sure that the problem is caused by something else. – Rob W Mar 15 '12 at 20:33
  • @RobW maybe you right.... it contain html code, this value can contain tags. If so, then how do I fix it ? Thanks for suggestion. – Jigberto Mar 15 '12 at 20:51

1 Answers1

1

Your title string contains quotes, newlines or backslashes. These characters have to be escaped:

var title = 'Your string was here, with a "quote" etc.';

// Escape each special character:
title = title.replace(/[\\"']/g, '\\$&') /* Backslash and quotes */
             .replace(/\n/g, '\\n')      /* Newlines             */
             .replace(/\r/g, '\\r')      /* Carriage returns     */
             .replace(/\t/g, '\\t')      /* Tabs                 */
             .replace(/\b/g, '\\f')      /* Backspace            */
             .replace(/\f/g, '\\b');     /* Form feed            */

chrome.tabs.executeScript(tabId, {code: "var param1='"+title+"'; var param2='value2'; "}, function(){ /*some code*/ })
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • I actually hard trouble with this, but instead compiled my code with closure compiler, with whitespace only compilation, and, then removed all newlines. – Devin Rhode Dec 07 '12 at 05:43
  • This code may throw errors for you if you're trying to clean javascript code to execute. You can to replace newlines, carriage returns, backspace and form feeds with a space character or no char ('') – Devin Rhode Dec 07 '12 at 06:30
  • @DevinGRhode "Cleaning JS code to execute"? Can you give an example? The answer will never fail for the circumstances as described by the question. Is your input a valid string? E.g. no unescaped line breaks before a string literal is terminated. – Rob W Dec 07 '12 at 10:22
  • I have a live extension update system, so I fetch a js file over the network, store in localStorage, and execute. The code is closure-compiled, which outputs newlines every ~500 chars. – Devin Rhode Dec 07 '12 at 22:41
  • If you're googling and have my scenario, where you need to eval javascript, checkout my answer here: http://stackoverflow.com/a/13775229/565877 – Devin Rhode Dec 08 '12 at 06:51
  • @DevinGRhode Can you give an example where the above code fails? – Rob W Dec 08 '12 at 08:50
  • I can't. I'm sure it does what it's supposed to just fine, but as I was googling, it was almost my answer, but not quite. – Devin Rhode Dec 08 '12 at 10:10
  • In my scenario, my js file simply had a newline, but this was replace by `\n` and `eval('\\n var = 22;')` is not valid js code. – Devin Rhode Dec 08 '12 at 11:11
  • @DevinGRhode In that case, the input string was directly passed to eval. You don't need to replace any characters at all. – Rob W Dec 08 '12 at 11:12