简体   繁体   English

我想在任何浏览器的开发控制台(JavaScript)中将网址列表加载到var

[英]I would like to load a list of urls to a var in any browsers' dev console (JavaScript)

I have a .txt file on my hard drive containing lots of URLs structured like this: 我的硬盘驱动器上有一个.txt文件,其中包含许多结构如下的URL:

http://url1.com/ http://url1.com/

http://url2.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. 我想将它们加载到Firefox / Chrome / IE的dev控制台中的var中,以便将其作为字符串的向量。 I plan to visit these pages with a for loop. 我计划使用for循环访问这些页面。 How can this be done? 如何才能做到这一点?

<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. 您可以使用代码生成此代码段,也可以只将文件导出一个全局变量,然后通过标签加载它。

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);
        }

You can read a file via JavaScript from the page . 您可以从页面通过JavaScript读取文件。 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. 我添加了一个抓取功能,可帮助您一次请求一个URL。

  <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 使用Javascript读取本地文本文件

The only way di make your JavaScript aware of local files is to HTTP GET them. 使JavaScript知道本地文件的唯一方法是HTTP GET它们。

So probably you have to put your file somewhere handy in the project folder and procees with an AJAX request. 因此,可能您必须将文件放在项目文件夹中的方便位置,并进行AJAX请求。

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")
}

I found a simple but not very elegant workaround for the issue. 我发现了一个简单但不是很好的解决方法。 I just copy and paste the list into a var definition. 我只是将列表复制并粘贴到var定义中。 I don't have to do this often, so it is kind of okay. 我不必经常这样做,所以还可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript关闭浏览器开发控制台? - how to close browsers dev console using javascript? Jquery / Javascript - 获取该会话的请求URL列表,如在浏览器控制台中 - Jquery / Javascript - get list of request URLs for that session like in browser console 我想在XPath中使用预先设置的javascript var。 这可能吗? - I would like to use a previously set javascript var inside of the XPath. Is this possible? 我希望能够通过单击超链接来加载/编辑 handlebars.js 模板(快速服务器),有什么建议吗? - I would like to be able to load/edit handlebars.js templates ( express server ) by clicking on a hyperlink , any suggestions? 我想以“波浪”形式加载图像。 - I would like to load images in a “wave”.. 是否可以在Chrome中从控制台加载外部javascript文件(从URL)? - Is it possible to load external javascript files (from URLs) from the console, in chrome? JavaScript表在加载/刷新时闪烁。 想知道我怎样才能避免这种情况发生? - JavaScript tables flicker on load/refresh. Would like to know how can I avoid this happening? 我想要一个使用javascript或其他可能方式的菜单子菜单 - I would like a menu submenu using javascript or any other way possible 我将如何仅使用JavaScript针对移动浏览器激活模式? - How would I activate a modal using JavaScript only for Mobile Browsers? Javascript 轮播工作,但不是我想要的 - Javascript carousel works, but not as I would like
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM