1
var stuff = "Heeeey";   
chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
    file: 'sendMessage.js'
});;

How can I pass the variable stuff to the file, sendMessage.js?

0

1 Answer 1

0

There is no way to pass a variable into the executed script. This is done I guess for security and performance reasons because this may introduce memory leaks.

What you can do is passing a value of the variable.

There are two ways:

  1. Send the message to your script, see the messaging for communication between extension and pages
  2. Inject the variable value into the page script using the same executeScript but passing the script as a string instead of the file name. You can do executeScript calls: one for value injections and another for file injection.
Sign up to request clarification or add additional context in comments.

7 Comments

Not sure if I got it to work. I'm attempting to follow #1. chrome.runtime.onMessage.addListener(function(message) { if (message.type == "pickLine") { pickLineText = message.content; } }); Is in my sendMessage.js and no matter what, the pickLine seems to always be blank
@ILovephp123 this is an asynchronous message, maybe you send it before the listener is registered on the page? Also you need to add all required permissions, last time I tried to do a similar thing I spent quite some time to make it work, it is not that obvious how they do it in chrome...
Does the sendMessage go above or below the execute shell?
I think there is no difference as long as both operations are asynchronous. I think you might need to wait for some milliseconds on the page to get the value and this is ugly. Most likely the second option is way more comfortable for simply setting the value
I feel like the second option you provided is a lot easier, I just don't really understand what you're saying lol. Sorry =/
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.