Bookmark text cross-domain

I need the user to go to a specific page with a specific div full of useful text. Then click on my bookmarklet and send the text in this div back to my server, which is different from the current domain. I successfully inserted jQuery into a bookmarklet click and selected the text. Now I need to figure out a way to send this cross domain to my server. I tried JSONP with jQuery and my text is too long for the url. My second idea was to open a new window and load the page from my domain, and then somehow insert the selected text into a new window, after which the user can click "Submit" and "POST" on my server. This did not help explain the reasons for javascript cross-references. Anyone have experience with this or ideas for this? Thanks.

+4
source share
3 answers

Create a form (with DOM) and POST data (you can target the iframe, but it will be fire and forget it).

+4
source

Here is an example of sending text to a remote server. And you can print javascript on the remote server if you want to change the page on which you have Iframe to say something like success. Or a pop-up message stating that it has ended. On a remote server, you can print this to create a popup:

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

On a webpage, you want to send information from 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> 
+3
source

Use javascript to create an iframe. Then add the form to the iframe and submit it. Once the form is submitted, the onload callback will light up.

 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); 
+2
source

Source: https://habr.com/ru/post/1300230/


All Articles