Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome extension chrome扩展中的不可见选项卡_Google Chrome Extension - Fatal编程技术网

Google chrome extension chrome扩展中的不可见选项卡

Google chrome extension chrome扩展中的不可见选项卡,google-chrome-extension,Google Chrome Extension,我基本上想通过使用Chrome扩展来自动化一个网站。但是这个网站有非常多的客户端代码,所以很难找到我需要哪些请求才能获得所需的信息 我能想到的最简单的方法是使用内容脚本在输入元素中输入文本并单击按钮,如下所示(本例中使用jQuery): 与这个问题非常相似,但与该网站的交互不应可见 因此:我可以从一个扩展插件中打开用户看不到的chrome页面,并使用它们与网站交互吗?请参见,您可以调用以下代码创建一个不可见的选项卡(非活动) 如果希望页面完全不可见,我认为唯一的选择是将其加载到背景页面上的ifr

我基本上想通过使用Chrome扩展来自动化一个网站。但是这个网站有非常多的客户端代码,所以很难找到我需要哪些请求才能获得所需的信息

我能想到的最简单的方法是使用内容脚本在输入元素中输入文本并单击按钮,如下所示(本例中使用jQuery):

与这个问题非常相似,但与该网站的交互不应可见

因此:我可以从一个扩展插件中打开用户看不到的chrome页面,并使用它们与网站交互吗?

请参见,您可以调用以下代码创建一个不可见的选项卡(非活动)


如果希望页面完全不可见,我认为唯一的选择是将其加载到背景页面上的iframe中。然后,您可以使用内容脚本访问iframe中的页面,就像访问任何普通可见页面一样

由于许多站点限制使用
X-Frame-Options
头进行嵌入,因此在将页面加载到iframe之前,您可能必须使用webRequest API删除该头。一些页面还使用其他技术来防止嵌入,这可能会使这一点更加复杂

示例代码:

manifest.json

{
  "manifest_version": 2,
  "name": "Hidden page in background",
  "description": "Interact with a hidden page in background",
  "version": "1.0",

  "background": {
    "page": "background.html",
    "persistent": true
  },
  "content_scripts": [
    {
      "matches": ["*://*.google.fr/*"],
      "js": ["contentscript.js"],
      "all_frames": true
    }
  ],
  "permissions": ["*://*.google.fr/*", "webRequest", "webRequestBlocking"]
}
background.html

contentscript.js

两个注释:

  • 删除
    X-Frame-Options
    标题不限于此处的背景页。它还允许在任何其他页面的iframe中嵌入相关页面。不幸的是,Chrome似乎不支持
    ALLOW-FROM uri
    值,该值可用于限制仅嵌入到扩展中
  • 正在将内容脚本注入此处的所有页面。您可以通过编程方式将其仅注入后台页面上的iframe,但这会变得更加复杂
  • 我以
    www.google.fr
    为例,因为它使用
    X-Frame-Options
    ,但不使用任何其他技术来阻止嵌入。我使用法语域名是因为
    google.com
    倾向于自动重定向到本地国家级域名

这仍然是一个可见的选项卡。我想做的事情通常可以使用无头浏览器完成,但不能在chrome扩展中完成。使用chrome.windows.create和“popup”可以创建一个无头浏览器。这正是我想要的。在我的例子中,我甚至不需要删除X-Frame-Options标题。这回答了你的问题吗?
chrome.tabs.create({ url: 'https://www.google.com', active: false, });
{
  "manifest_version": 2,
  "name": "Hidden page in background",
  "description": "Interact with a hidden page in background",
  "version": "1.0",

  "background": {
    "page": "background.html",
    "persistent": true
  },
  "content_scripts": [
    {
      "matches": ["*://*.google.fr/*"],
      "js": ["contentscript.js"],
      "all_frames": true
    }
  ],
  "permissions": ["*://*.google.fr/*", "webRequest", "webRequestBlocking"]
}
<!DOCTYPE html>
<html>
  <head>
    <script src="background.js"></script>
  </head>
  <body>
    <iframe id="iframe1" width="1000 px" height="600 px" src="http://www.google.fr"></iframe>
  </body>
</html>
// This is to remove X-Frame-Options header, if present
chrome.webRequest.onHeadersReceived.addListener(
    function(info) {
      var headers = info.responseHeaders;
      var index = headers.findIndex(x=>x.name.toLowerCase() == "x-frame-options");
      if (index !=-1) {
        headers.splice(index, 1);
      }
      return {responseHeaders: headers};
    },
    {
        urls: ['*://*.google.fr/*'], //
        types: ['sub_frame']
    },
    ['blocking', 'responseHeaders']
);
var elementToInsert = document.createElement("h1");
elementToInsert.textContent = "This text comes from my content script.";
document.body.insertBefore(elementToInsert, document.body.firstChild);