【问题标题】:Open tab in background and get data from it - Chrome extension在后台打开标签并从中获取数据 - Chrome 扩展
【发布时间】:2013-07-22 11:20:27
【问题描述】:

谁能告诉我如何在后台打开网址以及如何从中获取数据。我要打开的 URL 位于我也从中获取数据的页面中。我使用 XPath 来获取数据。 这是我的代码:

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'});

【问题讨论】:

标签: google-chrome google-chrome-extension


【解决方案1】:

“在后台打开标签页”是什么意思? 您是否想向用户隐藏后台发生某种页面获取的事实?如果是这样,我认为使用当前的 Chrome 扩展 API 是不可能的。

请检查这个问题:How to fetch javascript heavy pages from chrome extension

几个月前我也遇到过同样的问题。

如果您不关心用户是否注意到打开了一个新选项卡/窗口,那么只需创建新选项卡并且不要将焦点放在它上面。这样做很简单,请查看扩展文档。

顺便说一句,在我看来,Xpath 不是要走的路。我认为 CSS 更适合解析数据。

同时查看这个最近的答案:Chrome extension: How to open a link in new tab?

【讨论】:

猜你喜欢
  • 2011-06-26
  • 1970-01-01
  • 2012-03-23
  • 2016-06-19
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2020-08-03
相关资源
最近更新 更多