Skip to content

Instantly share code, notes, and snippets.

@fakhrulhilal
Last active September 22, 2017 14:38
Show Gist options
  • Save fakhrulhilal/5afe2c9aaef9b6e53315539e1aa9e549 to your computer and use it in GitHub Desktop.
Save fakhrulhilal/5afe2c9aaef9b6e53315539e1aa9e549 to your computer and use it in GitHub Desktop.
Get the driver list from lenovo. Useful for creating download list with download manager and generate file checksum
(function(d, w) {
var utility = {
trim: word => word.replace(/(^\s+|\s+$)/g, ''),
empty: word => !word || /^\s*$/.test(word),
createNode: (node, text) => {
let element = d.createElement(node);
if (text) element.innerText = text;
return element;
},
toArray: list => list instanceof Array ? list : Array.prototype.slice.call(list)
};
let thinkPad = {
getTitle(node) {
return node.querySelector('.file-description .file-title > a').innerText;
},
getLabels(node) {
return utility.toArray(node.querySelectorAll('.file-list .file-item .dc-link > span > a')).map(el => el.innerText);
},
getUrls(node) {
return utility.toArray(node.querySelectorAll('.file-list .file-item .dl-link-btn')).map(el => el.href);
},
getFilenames(node) {
let urls = this.getUrls(node);
let parser = /\/([^\/]+\.\w+)$/;
return urls.map(url => {
if (utility.empty(url)) return null;
let temp = parser.exec(url);
return temp != null && temp.length > 1 ? temp[1] : null;
});
},
getChecksums(node) {
let lines = utility.toArray(node.querySelectorAll('.file-list .file-item'));
return lines.map(line => {
let checksumElement = utility.toArray(line.querySelectorAll('.ds-check-sum .check-sum-block p.line'));
let checksums = {}
checksumElement.forEach(line => {
let text = line.innerText;
let splitted = text.split(':');
if (splitted.length < 2) return null;
checksums[splitted[0].trim()] = splitted[1].trim();
});
return checksums;
})
},
getDrivers(rows, exclude) {
let converted = utility.toArray(rows);
let drivers = [];
converted.forEach(row => {
let title = this.getTitle(row);
let labels = this.getLabels(row);
let urls = this.getUrls(row);
let filenames = this.getFilenames(row);
let checksums = this.getChecksums(row);
if (labels.length != urls.length &&
urls.length != filenames.length &&
filenames.length != checksums.length)
return;
for (let i = 0, length = labels.length; i < length; i++) {
if (exclude instanceof Function && exclude(title, labels[i], urls[i], filenames[i]))
continue;
drivers.push({
title,
label: labels[i],
url: urls[i],
filename: filenames[i],
checksums: checksums[i]
});
}
});
return drivers;
},
dump(data) {
while (d.body.firstChild) d.body.removeChild(d.body.firstChild);
// create table container and header
var table = d.createElement('table');
var head = d.createElement('thead');
var rowHead = d.createElement('tr');
rowHead.appendChild(utility.createNode('th', 'Label'));
rowHead.appendChild(utility.createNode('th', 'Title'));
rowHead.appendChild(utility.createNode('th', 'File name'));
rowHead.appendChild(utility.createNode('th', 'URL'));
rowHead.appendChild(utility.createNode('th', 'SHA1'));
rowHead.appendChild(utility.createNode('th', 'MD5'));
head.appendChild(rowHead);
table.appendChild(head);
// create row container and its children
var body = d.createElement('tbody');
data.forEach(item => {
var row = d.createElement('tr');
row.appendChild(utility.createNode('td', item.label));
row.appendChild(utility.createNode('td', item.title));
row.appendChild(utility.createNode('td', item.filename));
row.appendChild(utility.createNode('td', item.url));
row.appendChild(utility.createNode('td', item.checksums['SHA1']));
row.appendChild(utility.createNode('td', item.checksums['MD5']));
body.appendChild(row);
});
// wrap it up
table.appendChild(body);
d.body.appendChild(table);
}
};
let rows = d.querySelectorAll('section.row.filtered-list .main-listing .row.line-item');
let data = thinkPad.getDrivers(rows, (title, label, url, filename) => {
if (/^(user Guide)/i.test(label)) return true;
if (/^README/i.test(label)) return true;
if (/\.(pdf|txt)$/i.test(filename)) return true;
return false;
});
thinkPad.dump(data);
w.ThinkPad = thinkPad;
w.Utility = utility;
})(document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment