Chrome 扩展不是 运行 文档启动时的脚本而不是打印内容

Chrome Extension not running scripts on document start and not printing content

我正在开发一个小型 Chrome 扩展程序,我不希望它在您转到 google.com.au 时自动重定向到 "redirect.js",但它不起作用。如果我手动转到 "redirect.js" 中显示的 "blank.html" 文件,我的 "httpGet" 功能将不起作用。

manifest.json:

{
  "name": "Simon Extended",
  "description": "Beautify's Simon.",
  "version": "1.0",
  "manifest_version": 2,

    "icons": {
    "512": "icon.png"
  },

  "background": {
    "scripts": ["redirect.js"]
  },

  "browser_action": {
    "default_title": "Simon Extended"
  },

  "content_scripts": [
    {
      "matches": ["http://google.com.au/*", "https://google.com.au/*"],
      "js": ["redirect.js"],
      "run_at": "document_start"
    }
  ]
}

redirect.js:

function httpGet(theUrl)
{
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            return xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", theUrl, false );
    xmlhttp.send();    
}

chrome.browserAction.onClicked.addListener(function(tab) {
    window.open("blank.html")
    document.write(httpGet("http://google.com"))
});

blank.html:

<html>
    <head>
        <title>Blank</title>
    </head>
    <body>

    </body>
</html>
  1. 当您说 window.open("blank.html"); 时,您会打开一个新选项卡,其中 URL 是 您的 分机。
  2. 当你想要 write 那个 window 你必须说这样的话:

    var win = window.open("blank.html");
    win.document.write('some HTML');
    
  3. 由于 google.com 使用重定向到本地域,您必须将 ncr 添加到路径中: https://www.google.com/ncr
  4. 由于 XHR 请求是跨域的,因此您必须在目标域的 manifest.json 文件中添加权限。类似于:

    "permissions": [
        "http://www.google.com/*",
        "https://www.google.com/*",
    ]
    
  5. 因为这是 Chrome 的扩展,你知道它有 XMLHttpRequest,所以不需要检查。此外,您可以使用 onload 而不是 onreadystatechange 进行检查。

  6. 由于 WHATWG 和 Chrome 讨厌任何同步的东西,你必须 use async request:

    open("GET", url, true);
    
  7. 现在你的问题变成了你实际上并没有进行重定向,而是在 google.com 获取代码并将其写入你的 local html 扩展页面。因此,资源的任何相对路径(例如图像、CSS、JavaScript 文件等)都将无法访问。如果您想朝那个方向发展,则必须添加 <base>,(或解析并重写所有相关的 URL)。

你的最终 redirect.js 可能是这样的:

function http_set(url, win)
{
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
        var html = xhr.responseText.replace('<head>', '<head><base href="'+url+'">');
        win.document.write(html);
        win.document.close();
    }
    xhr.open("GET", url, true);
    xhr.send();
}

chrome.browserAction.onClicked.addListener(function(tab) {
    var win = window.open("blank.html");
    http_set("https://www.google.com/ncr", win);
});

直接去:

所以,不是 加载 来自 google.com 的 HTML 代码,你可以这样说:

chrome.browserAction.onClicked.addListener(function(tab) {
    window.open("https://www.google.com/ncr");
});

现在。这样做的效果是,当您单击扩展程序的图标时,会打开一个带有 google.com.

的新选项卡

真的直接去:

对于您要求的效果,您可以使用此 redirect.js 文件:

window.location = "https://www.google.com/ncr";

是的,这是整个文件。这样你就可以删除我们在清单文件中添加的权限,只添加触发域。

匹配URL

为此:您还需要匹配 www。例如:

  "matches": [
    "http://google.com.au/*",
    "https://google.com.au/*",
    "http://www.google.com.au/*",
    "https://www.google.com.au/*"
  ],

总而言之:

好的。说完所有这些之后,这可能不是解决问题的最佳方法。但我希望你至少学到了一两件事。

它的核心是:

  1. 练习并学习更多 JavaScript。作为语言的核心。
  2. 在为特定浏览器编写扩展时,您可以自由使用它拥有的实现 - 无需进行跨浏览器检查等。此外,您可以利用该特定浏览器库中的所有工具。
  3. 编写扩展时,您可以利用 API 提供的更多后端功能。当涉及到扩展时,许多事情通常以 有点不同的方式完成 - 因为您可以利用各种额外的工具。