3

Consider this code:

function SwapColumns() {
  chrome.tabs.executeScript(null,
      {code:"SwapNameAndSurname();"});
}

The code above is in popup.html, the method SwapNameAndSurname() is in a content script named js.js that contains nothing but a library full of functions. When I call the SwapColumns function from popup.html, the SwapNameAndSurname() function in js.js is called changing something in the DOM of the original page via the power of content scripts.

However my problem is here. I have prepared an object named employee in popup.html that I need to send to the SwapNameAndSurname() function as a parameter:

var employee = {'Name' : 'John', 'Surname' : 'Doe', 'Qualifications' : [{'Title':'MCAD', 'Date':'Jan 2008'},{'Title':'MCSA', 'Date':'Feb 2008'}]};

function SwapColumns() {
  chrome.tabs.executeScript(null,
      {code:"SwapNameAndSurname(**???employee???**);"});
}

How can I do that? Since in chrome.tabs.executeScript you have to write code directly into a string, you cannot really send parameters. I've read Chrome's tabs.executeScript - passing parameters and using libraries? but still can't figure out what's happening.

Community
  • 1
  • 1
prince
  • 671
  • 2
  • 11
  • 26

1 Answers1

1

I found an answer to my own question. From the extension pages you have to call the chrome.tabs.sendRequest method and from the the content scripts you have to add a listener using the chrome.extension.onRequest.addListener method. The send request method will trigger the event found in the content scripts enabling you to send any form of data to the content script.

For further reference see http://aarongusman.wordpress.com/2011/03/30/communication-between-chrome-extension-content-scripts-and-extension-pages/

prince
  • 671
  • 2
  • 11
  • 26