Soufiene
Soufiene

Reputation: 2218

Open tab in background and get data from it - Chrome extension

Can someone can give me how to open an url in background and how can I get data from it. The URL that I want to open is in a page that also I get data from it. I use XPath to get data. This is my code :

Popup.js

chrome.tabs.query({
active: true, 
currentWindow: true }, function(tabs){
if(tabs[0].url.indexOf("extranet.louvre-hotels.fr")!=-1)
{
    chrome.tabs.sendRequest(tabs[0].id, {
        action: "getDOM"
    }, function(response)

    {   
        chrome.tabs.getSelected(null, function(tab) {
    // get response
        });         
    });
}

});

Manifest.json

{
"manifest_version": 2,

"name": "SymaExtract",
"description": "SymaExtract",
"version": "1.0",
"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
},
"permissions": [
    "webNavigation",
    "tabs",
    "background",
    "http://localhost/extract.php",
    "http://*/*"

],
"background": {
    "scripts": ["background.js"]
},
"content_scripts": [
{
  "matches": ["http://*/*"],
  "js": ["dom.js"]
}

] }

Background.js

chrome.tabs.create({url: 'url_that_i_want_to_open'});

Upvotes: 8

Views: 12634

Answers (1)

Methos
Methos

Reputation: 14344

What do you mean by "open the tab in the background"? Do you want to hide the fact from the user that some kind of page fetching is happening in the background? If so, I think its not possible with current Chrome extension APIs.

Please check this question: How to fetch javascript heavy pages from chrome extension

I faced the same problem a few months ago.

If you don't care whether user notices that a new tab/window is opened, then just create new tab and do not set focus on it. Doing so is straightforward, check the extension documentation.

BTW, in my opinion, Xpath is not the way to go. I think CSS is better suited to parse the data.

Also check this recent answer: Chrome extension: How to open a link in new tab?

Upvotes: 3

Related Questions