Premt Premt - 1 month ago 19
HTML Question

NodeJS function not working

Hey am trying to add the parsed data to the table, but for some reason it's not getting added am still learning so I don't really know why I tried searching couldn't find anything

main.js:

const {app, BrowserWindow} = require('electron');

let win;


app.on('ready', () => {
win = new BrowserWindow({width: 1280, height: 1080});
win.loadURL(`file://${__dirname}/index.html`);
//win.setMenu(null);
win.webContents.openDevTools();

win.on('closed', () => {
win = null
})
});

// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
});


index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<LINK rel="stylesheet" href="design.css" type="text/css">
<title>Today's Shows</title>
<meta charset="UTF-8">
<title>Hello</title>
<script>window.$ = window.jQuery = require('./jquery.min.js');</script>
</head>
<body>

<div id="page" class="yt-card">
<section>
<div class="tbl-header">
</div>
<div class="tbl-content">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%" id="myTable">
<tbody>
</tbody>
</table>
</div>
</section>
</div>
<script>
var cheerio = require('cheerio'),
$ = cheerio.load(`
<tr name="hover" class="forum_header_border">
<td class="forum_thread_post" width="35" align="center"><a href="/shows/1458/hunted/" title="Hunted Torrent"><img src="/images/eztv_show_info3.png" alt="Info" title="Hunted Torrents" border="0"></a></td>
<td class="forum_thread_post">
<a href="/ep/161087/hunted-2015-s02e06-720p-hdtv-x264-c4tv/" title="Hunted 2015 S02E06 720p HDTV x264-C4TV [eztv] (960.91 MB)" class="epinfo">Hunted 2015 S02E06 720p HDTV x264-C4TV [eztv]</a>
</td>
<td class="forum_thread_post" align="center">
<a href="magnet:?xt=urn:btih:a489093124274c23d6f52d16c5dd73d5da018489&amp;dn=Hunted.2015.S02E06.720p.HDTV.x264-C4TV%5Beztv%5D.mkv%5Beztv%5D&amp;tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A80&amp;tr=udp%3A%2F%2Fglotorrents.pw%3A6969%2Fannounce&amp;tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&amp;tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&amp;tr=udp%3A%2F%2Fexodus.desync.com%3A6969" class="magnet" title="Hunted 2015 S02E06 720p HDTV x264-C4TV [eztv] (960.91 MB) Magnet Link" rel="nofollow"></a>
<a href="https://zoink.ch/torrent/Hunted.2015.S02E06.720p.HDTV.x264-C4TV[eztv].mkv.torrent" rel="nofollow" class="download_1" title="Hunted 2015 S02E06 720p HDTV x264-C4TV Torrent: Download Mirror #1"></a>
</td>
<td class="forum_thread_post" align="center">960.91 MB</td>
<td class="forum_thread_post" align="center">26m 58s</td>
<td class="forum_thread_post" align="center"><span style="color: green; ">3</span></td>
<td class="forum_thread_post_end" align="center"><a href="/forum/discuss/161087/" rel="nofollow" title="Discuss about Hunted 2015 S02E06 720p HDTV x264-C4TV [eztv]:"><img src="/ezimg/s/1/3/chat_empty.png" alt="Discuss" title="Discuss about this show" width="16" border="0" height="16"></a></td>
</tr>
<tr name="hover" class="forum_header_border">
<td class="forum_thread_post" width="35" align="center"><a href="/shows/1865/cant-pay-well-take-it-away/" title="Can't Pay? We'll Take It Away! Torrent"><img src="/images/eztv_show_info3.png" alt="Info" title="Can't Pay? We'll Take It Away! Torrents" border="0"></a></td>
<td class="forum_thread_post">
<a href="/ep/161084/cant-pay-well-take-it-away-s04e19-720p-hdtv-x264-c4tv/" title="Cant Pay Well Take It Away S04E19 720p HDTV x264-C4TV [eztv] (831.79 MB)" class="epinfo">Cant Pay Well Take It Away S04E19 720p HDTV x264-C4TV [eztv]</a>
</td>
</tr>`);

$('td.forum_thread_post').each(function () {

var tElement = $(this).next();
var dElement = tElement.next();
var sElement = dElement.next();
tElement = getTitle(tElement);

var title = tElement.text();
var website = tElement.attr('href');
var download = getDownload(dElement);
var size = sElement.text();

if (!isEmpty(title) && !isEmpty(download) && !isEmpty(size) && !isEmpty(website)) {
console.log(`Title: ${title}\n Download: ${download}\n Size:${size}\n Website: ${website}`);
AddRow(website,size,title,download);
}
});

function getDownload(dElement) {
if (!isEmpty(dElement)) {

for (var i = 1; i < 3; i++) {
var download = dElement.children(`a.download_${i}`).attr('href');
if (!isEmpty(download)) {
return download;
}
}
}
return null;
}

function getTitle(tElement) {
if (!isEmpty(tElement)) {
var title = tElement.children('a');
if (!isEmpty(title)) {
return title;
}
}
return null;
}

function isEmpty(value) {
return typeof value == 'string' && !value.trim() || typeof value == 'undefined' || value === null;
}

function AddRow(website,size,title,download) {
var newRowContent = `<tr class="torItem"><td width="65.1%"><a class="download" target="_blank" href="${website}">${title}</a></td> <td width="8.5%">${size}</td> <td width="12%">FROM</td> <td width="12.9%"><a class="downbutton" href="${download}">Download</a></td></tr>`;
$("#myTable").find("tbody").append(newRowContent);
}
</script>

</body>
</html>


Thank you.

Answer

Package cheerio is not for client side implementation. You could use jQuery for that.