简体   繁体   English

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

[英]Bookmarklet post html contents (instead get/request)

I'd like to create a bookmarklet to take the contents of a html page and send it to an external url for processing. 我想创建一个书签,以获取html页面的内容,并将其发送到外部url进行处理。

Usually it would be enough to just send document.title to the server and CURL it up serverside, but for various reasons this is not an option here. 通常,仅将document.title发送到服务器并在服务器端对其进行CURL就足够了,但是由于种种原因,这不是这里的选择。 I tried: 我试过了:

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 is just <? file_put_contents('result.html',$_REQUEST['html']); ?> grabhtml.php只是<? file_put_contents('result.html',$_REQUEST['html']); ?> <? file_put_contents('result.html',$_REQUEST['html']); ?>

As expected Apache doesn't like such long requests: 不出所料,Apache不喜欢这么长的请求:

Request-URI Too Large
The requested URL's length exceeds the capacity limit for this server.

Therefore I thought about sending the document.documentElement.innerHTML via POST instead of GET. 因此,我考虑过通过POST而不是GET发送document.documentElement.innerHTML。

Firefox-WebmasterTools has an option to show the "View Generated Source" rather than the normal "View Source". Firefox-WebmasterTools具有显示“查看生成的源代码”而不是常规“查看源代码”的选项。

I remember last year I've read an article about how an Instapaper-like-service did exactly the same. 我记得去年我读过一篇有关类似Instapaper的服务如何完全一样的文章。

I've searched for days for this article, or for bookmarklet examples which POST the "Generated Source" to my form. 我已经在几天内搜索了这篇文章,或者在将“生成的源代码”发布到我的表单中的小书签示例中进行了搜索。

My Javascript skills are very basic, but I'm a quick learner. 我的Javascript技能非常基础,但是我是一个快速学习的人。 A kick into the right direction would be greatly appreciated. 向正确的方向踢将不胜感激。

You can only use POST via AJAX, so it is necessary that your JS script is running on the same domain as grabhtml.php 您只能通过AJAX使用POST,因此您的JS脚本必须与grabhtml.php在同一域上运行

If it is, you can simply use jQuery for this, and it will look like: 如果是这样,您可以简单地使用jQuery,它看起来像:

$.post('grabhtml.php', {
    url: document.location,
    title: document.title,
    html: htmlstuff
}, function(response) {
    alert('Successfully posted.');
});

If don't, you could try to embed your script into iframe (running at the same domain as php script), send title, body, etc. from the parent frame to this iframe (via window.postMessage ) and issue POST request described above ommitting cross-domain restrictions. 如果不这样做,则可以尝试将脚本嵌入到iframe (与php脚本在同一域中运行),将标题,正文等从父框架发送到此iframe (通过window.postMessage ),然后发出描述的POST请求以上忽略了跨域限制。

You can read more about window.postMessage here: http://viget.com/extend/using-javascript-postmessage-to-talk-to-iframes 您可以在此处阅读有关window.postMessage的更多信息: http : //viget.com/extend/using-javascript-postmessage-to-talk-to-iframes

Note: I'm not sure about window.postMessage maximum message size 注意:我不确定window.postMessage的最大邮件大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM