[英]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.