NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name npmjs_weekly_downloads
// @namespace https://openuserjs.org/users/Bootta11
// @version 1.6
// @license MIT
// @description Add weeklydownloads on npmjs search list.
// @author Bootta11
// @match https://www.npmjs.com/*
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @copyright 2021, Bootta11 (https://openuserjs.org/users/Bootta11)
// @updateURL https://openuserjs.org/meta/Bootta11/npmjs_weekly_downloads.meta.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function () {
let $ = window.jQuery;
let foundPackageElements = $("#main section .items-end");
let titles = $(foundPackageElements).find("h3").toArray().map(function (x) {
return x.innerText;
});
function numberWithSpaces(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}
function getPackageDataPromise(packageName) {
return new Promise(function (resolve, reject) {
GM_xmlhttpRequest({
method: "GET",
url: "https://api.npmjs.org/downloads/point/last-week/" + packageName,
onload: function (response) {
const json = JSON.parse(response.response);
resolve(json);
}
});
});
}
function getPackagesData(titles) {
const promises = [];
titles.forEach(function (t) {
promises.push(getPackageDataPromise(t));
});
return Promise.all(promises);
}
function addWeeklyDownloads(packagesData) {
const keyedData = [];
packagesData.forEach(function (item) {
keyedData[item.package] = item;
});
foundPackageElements.each(function () {
const h3TitleElement = $(this).find("h3")[0];
const title = $(this).find("h3")[0].innerText;
const url = $(this).find("a")[0].href;
const downloadsWeeklyCount = keyedData[title] && keyedData[title].downloads ? keyedData[title].downloads : 0;
$(h3TitleElement).parent().parent().append("<span style='margin-left: auto' class='npmjswdCount'>[" + numberWithSpaces(downloadsWeeklyCount) + "]</span>");
});
}
function updatePackagesWeeklyDownloads(titles) {
getPackagesData(titles).then(function (newPackagesData) {
addWeeklyDownloads(newPackagesData);
})
}
function refreshPackages() {
foundPackageElements = $("#main section .items-end");
let newTitles = $(foundPackageElements).find("h3").toArray().map(function (x) {
return x.innerText;
});
if (titles.join(',') !== newTitles.join(',')) {
$(".npmjswdCount").remove();
updatePackagesWeeklyDownloads(newTitles);
}
titles = newTitles;
}
updatePackagesWeeklyDownloads(titles);
setInterval(function () {
refreshPackages();
}, 1000);
})();