0

I have a .txt file on my hard drive containing lots of URLs structured like this:

http://url1.com/

http://url2.com/

. . .

I want to load them to a var in Firefox's/Chrome's/IE's dev console so that it would be a vector of strings. I plan to visit these pages with a for loop. How can this be done?

  • 1
    Possible duplicate of [Read a local text file using Javascript](http://stackoverflow.com/questions/27522979/read-a-local-text-file-using-javascript) – scrappedcola Mar 13 '17 at 18:38
  • Read files are very limited in javascript. This can help you: http://stackoverflow.com/questions/14446447/javascript-read-local-text-file – Ricardo Pontual Mar 13 '17 at 18:43
  • I found a simple but not very elegant workaround for the issue. I just copy and paste the list into a var definition. I don't have to do this often, so it is kind of okay. – catfishery Mar 13 '17 at 19:37

5 Answers5

0
<script>

var urls = [
  'http://url1.com/',
  'http://url2.com/'
];

</script>

You can generate this snippet with code or just have your file export a global variable and then load it via tags.

adam-beck
  • 5,659
  • 5
  • 20
  • 34
0

You can get the contents of the file to show up in the Console with the below snippet.

 var file="file://C:/FileName.txt";

        function read(file)
        {
            var File = new XMLHttpRequest();
            File.open("GET", file, false);
            File.onreadystatechange = function ()
            {
                if(File.readyState === 4)
                {
                    if(File.status === 200 || File.status == 0)
                    {
                        var Text = File.responseText;
                        console.log(Text);
                    }
                }
            }
            File.send(null);
        }
CaptainHere
  • 698
  • 6
  • 14
  • If I get this code right, it adds the whole file to the 'Text' var. I tried to check the results with ' print(Text[2]) ' but it responded with undefined. – catfishery Mar 13 '17 at 19:14
  • It wont store an an array. just as a string. @catfishery – CaptainHere Mar 13 '17 at 19:15
  • if you want to extract the urls from the returned string, you can refer this http://stackoverflow.com/questions/11209016/javascript-extract-urls-from-string-inc-querystring-and-return-array – CaptainHere Mar 13 '17 at 19:20
0

You can read a file via JavaScript from the page. You cannot upload a file to the developer's console.

I then modified the code bellow a bit to help you further. I added a scrape function that will help you request each URL one at a time.

  <div id="page-wrapper">

        <h1>Text File Reader</h1>
        <div>
            Select a text file: 
            <input type="file" id="fileInput">
        </div>
        <pre id="fileDisplayArea"><pre>

    </div>
<script>
function scrape(urls) {
    url = urls.shift()
    $.get(function (url) {
        // get the url data here
        scrape(urls);
    });
}

window.onload = function() {
        var fileInput = document.getElementById('fileInput');
        var fileDisplayArea = document.getElementById('fileDisplayArea');

        fileInput.addEventListener('change', function(e) {
            var file = fileInput.files[0];
            var textType = /text.*/;

            if (file.type.match(textType)) {
                var reader = new FileReader();

                reader.onload = function(e) {
                    scrape(reader.result.split("\n"));
                }

                reader.readAsText(file);    
            } else {
                fileDisplayArea.innerText = "File not supported!"
            }
        });
}

</script>

Modified version of:

Read a local text file using Javascript

Community
  • 1
  • 1
Neil
  • 14,063
  • 3
  • 30
  • 51
0

The only way di make your JavaScript aware of local files is to HTTP GET them.

So probably you have to put your file somewhere handy in the project folder and procees with an AJAX request.

var httpRequest;
function makeRequest() { 
    httpRequest = new XMLHttpRequest(); 
    request.open("GET", "files/url.txt", false);      
    request.send(null); 
    saveArray(request.responseText);
}
var array = [];
saveArray(string){
    array = string.split("\n")
}
Phugo
  • 400
  • 1
  • 10
0

I found a simple but not very elegant workaround for the issue. I just copy and paste the list into a var definition. I don't have to do this often, so it is kind of okay.