Javascript Greasemonkey:转发整页内容
文档加载完成后,是否可以选择完整的页面内容,并将其以ajax post的形式发送到另一台服务器 我正在尝试测试这一点,我启动了两个php服务器:Javascript Greasemonkey:转发整页内容,javascript,php,jquery,ajax,greasemonkey,Javascript,Php,Jquery,Ajax,Greasemonkey,文档加载完成后,是否可以选择完整的页面内容,并将其以ajax post的形式发送到另一台服务器 我正在尝试测试这一点,我启动了两个php服务器: localhost:9000:此程序将接收从Greasemonkey脚本发送的数据,并将其保存到文件中。测试代码: index.php <?php if (isset($_POST) && count($_POST) > 0) { file_put_contents("SOMETHING_AND_IT_IS_POST
localhost:9000
:此程序将接收从Greasemonkey脚本发送的数据,并将其保存到文件中。测试代码:
index.php
<?php
if (isset($_POST) && count($_POST) > 0) {
file_put_contents("SOMETHING_AND_IT_IS_POST.txt","");
$data = var_export($_POST);
file_put_contents("POSTED.txt",$data);
} else {
file_put_contents("SOMETHING_BUT_NO_POST.txt",$data);
}
Greasemonkey脚本:
// ==UserScript==
// @name testajax
// @namespace test
// @include http://localhost:9001/post.html
// @version 1
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// ==/UserScript==
$( document ).ready(function() {
console.log( "Loaded!" );
GM_xmlhttpRequest({
method: "POST",
url: "http://localhost:9000/index.php",
data: $("body").html(),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
alert("POSTED!");
}
});
});
因此,在浏览器中,我打开
http://localhost:9001/post.html
,GM脚本启动,console.log正在工作,但是localhost:9000/index.php
没有接收任何内容。您必须以键
/值
对发送数据:
data: {html: $("body").html()},
然后在您的
$POST
超全局中查找传入的html
属性。好的,因此它不是上述问题的解决方案,更像是一种更好的方法
我试图用greasemonkey实现自动化,这就是为什么它试图将数据发送到另一个地方。有一个工具可以实现这一点,称为casperJS(构建在PhantomJS之上,运行它也需要它)。基本上,您可以编写JavaScript,在站点中导航,选择所需的数据,并使用casperjs的ajax util发布数据。所有这些都有很好的记录
它不使用
GM\u xmlHTTPRequest()
,仅使用$.ajax()
,它将发送请求,但我收到一篇空帖子,并且我收到一个错误(console.log):跨源请求被阻止:同源策略不允许在http://localhost:9000/index.php. 这可以通过将资源移动到同一域或启用CORS来解决。
Odd。。尝试用以下内容替换您的grant
行:@grant GM\u xmlhttpRequest
但是,GM的ajax不会做任何事情。它甚至不会发送帖子(在firefox的网络选项卡中查看)
data: {html: $("body").html()},