Sending Text Cross Domain By Bookmarklet

前端 未结 3 564
遥遥无期
遥遥无期 2020-12-15 14:11

I need a user to navigate to a certain page that has a certain div full of useful text. Then click my bookmarklet and send the text in that div back to my server, which is

相关标签:
3条回答
  • 2020-12-15 14:46

    Here is an example to post text to a remote server. And you can print javascript in the remote server if you want to change the page you have the Iframe on to say something like success. Or a popup saying it finished. On the remote server you can print this to make a popup:

    <script> parent.document.getElementById('postmessage').style.display='block'; parent.alert('Successfully posted');</script>
    

    On the webpage you want to send information from make a form and Iframe like this.

    <span id="postmessage" style="display:none">Success Message</span>
    <form action="http://www.remoteserver.com/upload_test.php" method="post" target="post_to_iframe">
      <input type="hidden" value="the text to send to remote server" />
      <input type="submit" value="Submit" />
    </form>
    
    <!-- When you submit the form it will submit to this iFrame without refreshing the page and it will make the popup and display the message. -->
    <iframe name="post_to_iframe" style="width: 600px; height: 500px;"></iframe>
    
    0 讨论(0)
  • 2020-12-15 14:52

    Generate a form (with DOM) and POST the data (you might want to target an iframe, but it will be fire and forget).

    0 讨论(0)
  • 2020-12-15 14:56

    Use javascript to create an iframe. Then add a form to the iframe and submit it. Once the form has been submitted, the onload callback will fire.

    var i=document.createElement('iframe');
    i.setAttribute('name', 'frame-id');
    i.setAttribute('id', 'frame-id');
    i.setAttribute('allowtransparency', 'true');
    i.setAttribute('style', 'border: 0; width: 1px; height: 1px; position: absolute; left: 0; top: 0;');
    i.setAttribute('onload', 'iframeFormSubmitted();');
    
    document.body.appendChild(i);
    
    var html = '<html><body>' +
    '<form action="/post_url" method="post" id="iframe-form" accept-charset="utf-8">' +
    '<input type="hidden" name="id" value="' + your_text + '"/>' +
    '</form>' +
    '<scr'+"ipt>var e=encodeURIComponent,w=window,d=document,f=d.getElementById('f');" +
    "d.getElementById('iframe-form').submit();" +
    '</scr'+"ipt></body></html>";
    
    window.frames['frame-id'].document.write(html);
    
    0 讨论(0)
提交回复
热议问题