8

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'});
Soufiene
  • 2,218
  • 3
  • 14
  • 15
  • 1
    Possible duplicate of [Chrome desktop notification for dynamic site update when chrome runs in background](http://stackoverflow.com/q/16343320/710446), in which the asker wants to fetch page contents and examine them in a background page. Then again, might not be an exact duplicate, because I'm not 100% clear what you want to do (but that link may still be helpful). – apsillers Jul 23 '13 at 18:53
  • The question is ambiguous. What do you mean by "in background"? This might be what you were looking for: [Chrome extension: loading a hidden page (without iframe)](https://stackoverflow.com/questions/17299322/chrome-extension-loading-a-hidden-page-without-iframe) – Tamás Bolvári Dec 15 '19 at 21:11

1 Answers1

3

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?

Community
  • 1
  • 1
Methos
  • 13,608
  • 11
  • 46
  • 49
  • Great answer. This might be relevant too: [Chrome extension: loading a hidden page (without iframe)](https://stackoverflow.com/questions/17299322/chrome-extension-loading-a-hidden-page-without-iframe) – Tamás Bolvári Dec 15 '19 at 21:14