我想创建一个书签来获取 html 页面的内容并将其发送到外部网址进行处理。
通常,只需将 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']); ?>
正如所料,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 最大消息大小
关于javascript - 书签发布 html 内容(而不是获取/请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085593/