Can I use JavaScript to insert in place some plaintext from a remote file by URL?

0

So, I'm not sure if the reason I'm having trouble with this is because it looks awfully like malicious activity or not. What I'm attempting to do is make my life a little less terrible with a piece of very inflexible and outdated web-based software.

Basically, there's a block of HTML that I can edit from within the software (it's inserted into a td element), and I want to have the content of that block come from a plaintext file on my local drive, being synced to the cloud with Dropbox.

It is possible to do something like this:

<td>
  <script>
    insert_text_from_url("https://dl.dropboxusercontent.com/s/blahblahblah/file.txt?token_hash=blahblahblah&dl=1");
  </script>
</td>

With the output being <td>[contentsof_file.txt]</td>?

NReilingh

Posted 2013-08-26T15:57:22.647

Reputation: 5 539

Would <iframe> work? You can do what you want with javascript, but it's not a simple, single function. – Darth Android – 2013-08-26T16:06:08.887

I was hoping not to use an iframe since it's much less elegant, but I'm somehow not able to get that to work either. – NReilingh – 2013-08-26T16:09:23.120

XSS is prevented by most browser JavaScript implementations. – Synetech – 2013-12-31T05:53:39.390

@Synetech Yes, this was my issue in testing, since the browser realized it was loading the same code being submitted in the previous page load. But, all the browsers I've tested have been okay loading a .js file from a remote server as long as everything is under SSL. – NReilingh – 2013-12-31T05:55:51.130

Answers

0

Rather than load a remote plaintext file, it turned out to be much easier to make the remote file a JavaScript "wrapper" that had the plaintext inlined using a rather-hackish "multiline text variable":

remote_editable_file.js:

var myString = function(){/*

Editable plaintext or whatever goes here!
Multiline, too!

*/}.toString().slice(15,-4);
document.getElementById('content').innerHTML = myString;

The last line is what does all the work; it's simple enough that jQuery is overkill unless you run into insurmountable portability issues.

In the originating page (the block of HTML that is only editable manually), all you then need is:

<div id="content"></div>
<script src="https://dropbox/link/to/remote_editable_file.js" type="text/javascript"></script>

NReilingh

Posted 2013-08-26T15:57:22.647

Reputation: 5 539

The last line does nothing more than to assign the contents of the specified element. Your answer is confusing because you said you wanted the contents of the file to be inserted, but nowhere in it have you specified a URL. Where are the contents of the file coming from? ಠ_ఠ Unless I’ve suddenly lost all knowledge of computers, I don’t see how your answer could possibly accomplish what you had asked. – Synetech – 2013-12-31T05:57:29.467

@Synetech Clarified. – NReilingh – 2013-12-31T06:00:09.023

…it was loading the same code being submitted in the previous page load… In the originating page… Actually it’s more confusing now. What/where is this other page? I still don’t see where the .txt file or its contents come in. – Synetech – 2013-12-31T06:03:20.927

@Synetech Sorry; edited again. Give it one more try? – NReilingh – 2013-12-31T06:11:46.400

So in other words, you gave up trying to load an external file and just put it directly in the .js file? In that case, why bother with a separate file? Why not just put it directly in the .html file? Is it something that would be used in multiple files? – Synetech – 2013-12-31T06:19:33.397

@Synetech I thought it was clear from the question (maybe it wasn't), but the idea here is that there's not-easily-editable HTML that is output by this piece of software, and then an easily-editable-file synced to the cloud with dropbox, and included somehow into the page output by the software. The original ask was looking for static code that would import a text file; this is just shifting things around so that what is being imported is both the code and the text. – NReilingh – 2013-12-31T06:26:08.337

1

You can accomplish this with JQuery. You'll want to have a way you can lookup your td, like giving it an id:

<td id="insertTextHere">
</td>
<script>
    $(function(){
          $('#insertTextHere').load('https://dl.dropboxusercontent.com/s/blahblahblah/file.txt?token_hash=blahblahblah&dl=1');
    });
</script>

heavyd

Posted 2013-08-26T15:57:22.647

Reputation: 54 755