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

7
  • What is the value of tabId? Also, in case you didn't know: chrome.tabs.executeScript will not work in Content scripts. Commented Mar 15, 2012 at 17:58
  • i am not sure that i understand the question, can you give an example of what output you desire from this? Commented Mar 15, 2012 at 17:59
  • Above code works good... but param1 value is undefined.. because it not pass. Commented Mar 15, 2012 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. Commented Mar 15, 2012 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*/ }); Commented Mar 15, 2012 at 20:27

1 Answer 1

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*/ })
Sign up to request clarification or add additional context in comments.

9 Comments

I actually hard trouble with this, but instead compiled my code with closure compiler, with whitespace only compilation, and, then removed all newlines.
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 ('')
@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.
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.
If you're googling and have my scenario, where you need to eval javascript, checkout my answer here: stackoverflow.com/a/13775229/565877
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.