从url检索数据时如何绕过CORS策略?



我正在研究一种工具,该工具将在上传的CSV文件中读取url,并搜索url的每个文本内容,并计算页面的总单词数和"saas"的总出现次数。或";software"。下载按钮应该创建一个新的CSV文件,其中第1列中包含现有的url,第2列中包含总字数,第3列中包含总出现次数。url将根据出现次数最多到最少,如果url包含关键字"saas"或";software"应该用绿色突出显示,否则用红色突出显示。

这就是我写的代码:

type hereconst myFileInput = document.getElementById("myFileInput"); // get the input element
let urls = []; // initialize an empty array to store the URLs from the CSV file
let arrayNum = []; // initialize an empty array to store the word counts and "saas"/"software" occurrences
// function to extract URLs from the CSV file
function extractUrlsFromCsv(file) {
const reader = new FileReader(); // initialize a FileReader to read the uploaded CSV file
reader.onload = function() {
const lines = reader.result.split("n"); // split the CSV file into lines
for (let i = 0; i < lines.length; i++) {
urls.push(lines[i].split(",")[0]); // extract the first column (URLs) from each line and add it to the urls array
}
processUrls(); // call the processUrls function to start processing the URLs
};
reader.readAsText(file); // read the uploaded file as text
}
// function to process the URLs
function processUrls() {
if (urls.length === 0) {
createCsv(); // if there are no more URLs to process, call the createCsv function
return;
}
const url = urls.shift(); // get the first URL from the urls array and remove it from the array
fetch(url)
.then(response => response.text())
.then(text => {
const words = text.split(" "); // split the text into words
const wordCount = words.length; // get the total number of words
const saasOrSoftwareCount = words.filter(word => word.toLowerCase() === "saas" || word.toLowerCase() === "software").length; // get the total number of "saas" or "software" occurrences
arrayNum.push([url, wordCount, saasOrSoftwareCount]); // add the URL, word count, and "saas"/"software" count to the arrayNum array
processUrls(); // call the processUrls function again to process the next URL
})
.catch(error => {
console.log(`Error fetching ${url}: ${error}`); // log any errors
processUrls(); // call the processUrls function again to process the next URL
});
}
// function to create the CSV file
function createCsv() {
arrayNum.sort((a, b) => b[2] - a[2]); // sort the arrayNum array in descending order of "saas"/"software" occurrences
let csvContent = "data:text/csv;charset=utf-8,";
csvContent += "URL,Total Word Count,"saas"/"software" Countn"; // add the column headers to the CSV content
arrayNum.forEach(row => {
const url = row[0];
const wordCount = row[1];
const saasOrSoftwareCount = row[2];
const rowContent = `${url},${wordCount},${saasOrSoftwareCount}n`; // create a row of CSV content for each URL
csvContent += rowContent;
});
const encodedCsvContent = encodeURI(csvContent); // encode the CSV content as a URI
const downloadLink = document.createElement("a");
downloadLink.setAttribute("href", encodedCsvContent);
downloadLink.setAttribute("download", "urls.csv");
document.body.appendChild(downloadLink);
downloadLink.click(); // create a link with the encoded CSV content and click on it to trigger the download
}
myFileInput.addEventListener("change", event => {
const file = event.target.files[0]; // get the uploaded

你不能。至少不是在你的前端代码上。要么在服务器上设置适当的CORS规则,要么(如果第一种不可能)为请求设置一个后端"代理"。

相关内容

  • 没有找到相关文章

最新更新