google-chrome - 来自 Google Chrome 中用户脚本的跨源 XHR

标签 google-chrome cross-domain userscripts

有没有人有幸从 Google Chrome 中的用户脚本执行跨源 XHR?请求通过服务器(我可以在日志中看到它们)但是,readystatechanged事件永远不会被触发。

扩展权限似乎不起作用。 JSONP 也不是。

最佳答案

当前版本的 Chrome(13.0.781 或更高版本)现在支持大部分或全部 GM_xmlhttpRequest() Doc功能--包括跨域请求 .
Issue 18857: Support cross-site XMLHttpRequest in content scripts .

所以这个脚本现在在 Chrome(当然还有 Firefox)上运行得非常好:

// ==UserScript==
// @name            _Cross domain (XSS) GM_xmlhttpRequest, Chrome too
// @include         http://stackoverflow.com/*
// @grant           GM_xmlhttpRequest
// ==/UserScript==

GM_xmlhttpRequest ( {
    method:     "GET",
    url:        "http://www.google.com/",
    onload:     function (response) {
                    console.log (   response.status,
                                    response.responseText.substring (0, 80)
                                );
                }
} );

(安装该脚本,然后浏览任何 SO 页面。该脚本会将 Google 主页的前 80 个字符写入控制台。)

关于google-chrome - 来自 Google Chrome 中用户脚本的跨源 XHR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936639/

相关文章:

header - header 来源与主机

javascript - 将innerHTML 抓取到变量中

google-analytics - 从先前的Google Analytics(分析)实现切换到跨域跟踪

jQuery POST 请求 - 在 &lt;iframe&gt; 内提交 <form>

javascript - 用于替换网页中单词的用户脚本

jquery-selectors - 在用户脚本中,使用 querySelector() 与 jQuery 的 $() 有什么优势吗?

javascript - onClick Window.Location 在 Chromium(Google Chrome 的 Linux 版本)中不起作用

javascript - 使用JavaScript生成JavaScript生成内联样式时浏览器不一致

javascript - 单击按钮替换输入字段中的选定文本

html - 图片在 Chrome 中加载,但在 Firefox 中不加载