0
A little JavaScript Help???
var fs = require('fs'), childProcess = require('child_process'), xml2js = require('xml2js'); var parser = new xml2js.Parser(); fs.readFile(__dirname + '/subscription_manager.xml', function (err, data) { parser.parseString(data, function (err, result) { var nodes = result.opml.body[0].outline[0].outline; nodes.forEach(function (node, index) { var url = node['
#x27;].xmlUrl; url = url.substring(url.indexOf('=') + 1, url.length); var channel = 'https://www.youtube.com/channel/' + url; if (index === 1) { childProcess.exec('start chrome ' + channel); console.log(result, index, channel); } }); }); }); In this code, JavaScript is used to open a youtube channel link from the downloaded "subscription_manager.xml" file in a new Google Chrome tab. The if statement with "index === 1" is a safeguard (because I have 220 subscriptions) so that it only opens one tab. I am trying to get it to open at least 10 tabs but every time I adjust the statement it gets ignored no matter what amount I enter, so instead it tries to open all the links and uses all the RAM and crashes my PC. When I change "index<=1" to "index<=10" using for, do-while, and while loops, the terminal logs 10 copies of the all the links. So 2200 lines of URL's, when changed back to "index<=1" it logs one of each link. I only need it to open a portion of the file, but if I set the number to anything other than 1 it just glitches. Can somebody please show me how to fix this?3 ответов
+ 4
require() is not a standard (built-in) JS function... NodeJS or CommonJS server sided frameworks have, and there's some libraries implementation of it: we cannot run/test your without knowing wich implementation you're using ^^
Anyway, where and how do you use loops in your implementation of more than one item treatment? Because I think you doesn't need such loop, as .forEach() method do it implicitly: it run the callback function for each item of an array, passing item value and index as argument ;P
So, I think you just have to modify the condition without adding loop statement:
if (index<10)
... would get you the first ten items of 'nodes', no more, no less (don't forgot that indexes start at 0)
+ 1
@Visph
I feel really stupid for not trying this already. It now works perfectly. Thanks m8.
I was struggling to figure this out for like 3 days now lol.
btw I was using NodeJS.
0
I can't spot the error, but perhaps you could delete the values/links from the nodes array so they can't be opened. (Copy the array if you want to keep them stored.)
Also, try logging JSON.stringify(nodes) to see how the array is structured, and make sure you are referencing the values properly.