![](/img/trans.png)
[英]Basic method to Add html content to the page with Greasemonkey?
[英]Greasemonkey: Send full page content forward
文檔加載就緒后是否可以選擇整頁內容並將其發送到另一台服務器?
我試圖測試這個,我啟動了兩個php服務器:
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);
}
localhost:9001
:模仿數據源,只托管一個html文件
post.html
<html>
<head>
</head>
<body>
<p>Test</p>
<div>
<p> should work</p>
</div>
<!--<form action="" method="post">
<p>Is commenting ok?</p>
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form> -->
</body>
</html>
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
沒有收到任何內容。
好的,所以它不是上述問題的解決方案,更像是一種更好的方法。
我嘗試用greasemonkey進行自動化,這就是為什么它試圖將數據發送到另一個地方。 有一個工具,稱為casperJS(內置於PhantomJS,它也需要運行它)。 基本上,您可以編寫JavaScript,在站點中導航,選擇所需的數據並使用casperjs的ajax util發布。 所有這些都記錄得很好。
您必須將數據作為key
/ value
對發送:
data: {html: $("body").html()},
然后在$POST
超全局中查找傳入的html
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.