书签发布 html 内容(而不是获取/请求)



我想创建一个书签来获取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']); ?>

正如预期的那样,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 的最大邮件大小

最新更新