Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Text Cross Domain By Bookmarklet

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 different from the current domain. I have successfully inserted jQuery on the bookmarklet click and selected the text. Now I need to figure out a way to send that text 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 up a new window and load a page from my domain, and then somehow insert the selected text into the new window, after which the user could click submit and POST that data to my server. This didn't work for javascript cross-site reasons. Anyone have any experience with this or ideas for doing this? Thanks.

like image 636
agoessling Avatar asked Dec 17 '22 03:12

agoessling


2 Answers

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

like image 137
Quentin Avatar answered Dec 28 '22 07:12

Quentin


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>
like image 45
PHPGuru Avatar answered Dec 28 '22 07:12

PHPGuru