gil-dation
gil-dation

Reputation: 13

chrome extension, pass data from content to background

I've created a chrome extension that consists of manifest.json, content.js and background.js. in content.js, I'm extracting the current tab's URL and in background.js, I'm opening a new tab. what I want to do, which doesn't work is to pass the URL from content and append it to the URL that I'm calling in background.

content.js:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse)
{
    if(request.greeting=="gimmieyodatas")
    {
    var output ="URL=";
    //check for the character '?' for any parameters in the URL
    var paramIndex = document.URL.indexOf("?");
    //if found, eliminate the parameters in the URL
    if (paramIndex > -1)
    {
        output += document.URL.substring(0, paramIndex);
    };
        sendResponse({data: output});
    }
    else{
        sendResponse({}); 
    }
});

background.js:

var output2;
chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting:"gimmieyodatas"}, function(response) {
    output2 = response.data;
  });
});
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.create({url: "http://www.google.com?" + output2}, function(tab) {
        chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() {
            sendMessage();
        });
    });
});

When I run the extension from an open tab, it opens google on a new tab, but it doesn't append the current tab's URL in google URL, meaning the 'output' data does not get passed to the background.js. What am I doing wrong?

Upvotes: 1

Views: 5022

Answers (1)

Jude Osborn
Jude Osborn

Reputation: 1798

The problem is that you are not telling the background page to send a message when a new tab is opened. The call to chrome.tabs.getSelected only happens once when the extension is first run -- it does not happen every time a new tab is opened.

You're on the right track by using the background page as an intermediary between the two content pages, but I suggest a different approach:

Load the content script every time a new tab is opened, via the manifest file:

"content_scripts": [
    {
        "matches" : [
            "<all_urls>"
        ],
        "js" : [
            "content.js"
        ]
    }
],

Use a much simpler content script that just sends a message to the background with the current URL page as soon as it loads:

(content.js)

var paramIndex = document.URL.indexOf('?');
if (paramIndex > -1) {
    chrome.runtime.sendMessage({output2: 'URL=' + document.URL.substring(0, paramIndex)});
}

When the background page receives the message it saves the URL to a global variable:

(background.js)

var output2;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    output2 = request.output2;
});

You can then load that URL when the action button is clicked:

(background.js)

chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.create({url: "http://www.google.com?" + output2});
});

Upvotes: 4

Related Questions