Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's not such a thing as "pass a parameter to a file".</p> <p>What you <em>can</em> do is to either insert a content script <em>before</em> executing the file, or sending a message <em>after</em> inserting the file. I will show an example for these distinct methods below.</p> <h1>Set parameters before execution of the JS file</h1> <p>If you want to define some variables before inserting the file, just nest <a href="https://developer.chrome.com/extensions/tabs.html#method-executeScript"><code>chrome.tabs.executeScript</code></a> calls:</p> <pre><code>chrome.tabs.executeScript(tab.id, { code: 'var config = 1;' }, function() { chrome.tabs.executeScript(tab.id, {file: 'content.js'}); }); </code></pre> <p>If your variable is not as simple, then I recommend to use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify</code></a> to turn an object in a string:</p> <pre><code>var config = {somebigobject: 'complicated value'}; chrome.tabs.executeScript(tab.id, { code: 'var config = ' + JSON.stringify(config) }, function() { chrome.tabs.executeScript(tab.id, {file: 'content.js'}); }); </code></pre> <p>With the previous method, the variables can be used in <code>content.js</code> in the following way:</p> <pre><code>// content.js alert('Example:' + config); </code></pre> <h1>Set parameters after execution of the JS file</h1> <p>The previous method can be used to set parameters after the JS file. Instead of defining variables directly in the global scope, you can use the <a href="https://developer.chrome.com/extensions/messaging.html">message passing API</a> to pass parameters:</p> <pre><code>chrome.tabs.executeScript(tab.id, {file: 'content.js'}, function() { chrome.tabs.sendMessage(tab.id, 'whatever value; String, object, whatever'); }); </code></pre> <p>In the content script (<code>content.js</code>), you can listen for these messages using the <a href="https://developer.chrome.com/extensions/runtime.html#event-onMessage"><code>chrome.runtime.onMessage</code></a> event, and handle the message:</p> <pre><code>chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { // Handle message. // In this example, message === 'whatever value; String, object, whatever' }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload