[英]Bookmarklet post html contents (instead get/request)
我想創建一個書簽,以獲取html頁面的內容,並將其發送到外部url進行處理。
通常,僅將document.title發送到服務器並在服務器端對其進行CURL就足夠了,但是由於種種原因,這不是這里的選擇。 我試過了:
javascript:
(
function()
{
var htmlstuff=document.documentElement.innerHTML;
window.open
(
'http://127.0.0.1/grabhtml.php?url='+escape(document.location)+'&title='+escape(document.title)+'&html='+escape(htmlstuff)+'&lm='+escape(document.lastModified)
,'InfoDialog','menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,dependent=no,width=400,height=480,left=50,top=50'
);
}
)
();
grabhtml.php只是<? file_put_contents('result.html',$_REQUEST['html']); ?>
<? file_put_contents('result.html',$_REQUEST['html']); ?>
不出所料,Apache不喜歡這么長的請求:
Request-URI Too Large
The requested URL's length exceeds the capacity limit for this server.
因此,我考慮過通過POST而不是GET發送document.documentElement.innerHTML。
Firefox-WebmasterTools具有顯示“查看生成的源代碼”而不是常規“查看源代碼”的選項。
我記得去年我讀過一篇有關類似Instapaper的服務如何完全一樣的文章。
我已經在幾天內搜索了這篇文章,或者在將“生成的源代碼”發布到我的表單中的小書簽示例中進行了搜索。
我的Javascript技能非常基礎,但是我是一個快速學習的人。 向正確的方向踢將不勝感激。
您只能通過AJAX使用POST,因此您的JS腳本必須與grabhtml.php
在同一域上運行
如果是這樣,您可以簡單地使用jQuery,它看起來像:
$.post('grabhtml.php', {
url: document.location,
title: document.title,
html: htmlstuff
}, function(response) {
alert('Successfully posted.');
});
如果不這樣做,則可以嘗試將腳本嵌入到iframe
(與php腳本在同一域中運行),將標題,正文等從父框架發送到此iframe
(通過window.postMessage
),然后發出描述的POST請求以上忽略了跨域限制。
您可以在此處閱讀有關window.postMessage的更多信息: http : //viget.com/extend/using-javascript-postmessage-to-talk-to-iframes
注意:我不確定window.postMessage的最大郵件大小
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.