相关疑难解决方法(0)

Chrome扩展中的跨源XMLHttpRequest

根据chrome扩展API,如果设置了权限,则应允许使用XMLHttpRequest对象进行跨域调用:

扩展可以与其来源之外的远程服务器通信,只要它首先请求跨源权限即可.

我正在密切关注本教程,但下面的代码给出了一条错误消息:

XMLHttpRequest无法加载http://www.google.com/search?hl=en&q=ajax.原始chrome-extension:// bmehmboknpnjgjbmiaoidkkjfcgiimbo不允许使用Access-Control-Allow-Origin.

我不仅允许google.com的请求,而且要求任何网站但仍然无法通过.有人可以帮忙吗?

我的清单文件:

{
  "name": "The popup",
  "version": "0.1",
  "popup": "popup.html",
  "permissions": [
    "http://*/*",
    "https://*/*",
    "https://www.google.com/*",
    "http://www.google.com/*"
    ],
  "browser_action": {
    "default_icon": "clock-19.png",
    "default_title": "This is title",
    "default_popup": "popup.html"
  }
}
Run Code Online (Sandbox Code Playgroud)

实际的电话:

function sendRequest() {
    document.write("Sending request");
    var req = new XMLHttpRequest();
      req.open("GET", "http://www.google.com/search?hl=en&q=ajax", true);
      req.onreadystatechange = function() {
          if (req.readyState == 4) {
            if (req.status == 200) {
              alert(req.responseText);
              document.write("OK");
            }
          }
        };
      req.send();
} 
Run Code Online (Sandbox Code Playgroud)

javascript ajax google-chrome xmlhttprequest google-chrome-extension

11
推荐指数
1
解决办法
3万
查看次数

如何避免Chrome Web扩展程序中的跨域读取阻止(CORB)

我编写了chrome网站扩展程序,以避免在开发自己的网络应用程序时出现CORS限制。该扩展程序是开发人员的工具,用于将请求从源URL代理到目标URL。

这样的扩展核心代码,因此开发人员可以在我的网站上开发其页面并向其服务器端请求,而不受CORS的限制:

chrome.webRequest.onBeforeRequest.addListener(details => {
  let redirectUrl = '';
  //...
  redirectUrl = details.url.replace(TNT.validRules[i].source, TNT.validRules[i].dest);
 return {redirectUrl}
}, {urls: ['<all_urls>']}, ['blocking']);


chrome.webRequest.onHeadersReceived.addListener(details => {
  details.responseHeaders.map(item => {
    if (item.name.toLowerCase() == 'Access-Control-Allow-Origin'.toLowerCase()) {
      item.value = '*'
    }
  })
  return {responseHeaders};
}, {urls: ['<all_urls>']}, ["blocking", "responseHeaders"]);
Run Code Online (Sandbox Code Playgroud)

但是最新的Chrome 72无法代理该请求。控制台错误是:

跨域读取阻止(CORB) 使用MIME类型application / json 阻止了跨域响应 https://xxxxxxx.com/abc.json?siteId=69。有关更多详细信息,请参见 https://www.chromestatus.com/feature/5629709824032768

google-chrome-extension cross-origin-read-blocking

8
推荐指数
2
解决办法
1万
查看次数

如何在扩展弹出窗口中加载外部JavaScript

我正在尝试构建页面操作扩展,并需要从弹出窗口加载外部JavaScript库(它需要来自外部域,以便发送正确的cookie).

但是我收到此错误消息:

由于Content-Security-Policy,拒绝从'http://api.flattr.com/js/0.6/load.js?mode=auto'加载脚本.

有没有办法解决?

google-chrome-extension

7
推荐指数
1
解决办法
2551
查看次数