Last active
December 17, 2020 13:38
-
-
Save azu/ec22f3def09563ece54f8dc32523b152 to your computer and use it in GitHub Desktop.
Greamonkey Scripts: detect npm package support TypeScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name npm: package support types | |
// @namespace info.efcl | |
// @match https://www.npmjs.com/package/* | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
// License: MIT © azu | |
const NAME = "$Greasemonkey:package support types$"; | |
const fetchPackage = (packageName) => { | |
return new Promise((resolve, reject) => { | |
// https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md#getpackageversion | |
// @scope/name => @scope%2Fname | |
const path = packageName.replace(/\//g, "%2F"); | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: `https://registry.npmjs.com/${path}`, | |
onload: function(response) { | |
try { | |
resolve(JSON.parse(response.responseText)); | |
} catch (error) { | |
reject(error); | |
} | |
}, | |
onerror: function(response) { | |
reject(new Error(response.responseText)); | |
} | |
}); | |
}); | |
}; | |
const insertToTop = (message) => { | |
const top = document.querySelector("#top"); | |
if (!top) { | |
throw new Error(NAME + " Not found #top"); | |
} | |
const p = document.createElement("p"); | |
const span = document.createElement("span"); | |
span.setAttribute("style", "color:green"); | |
span.textContent = "TYPE: "; | |
p.append(span); | |
p.append(document.createTextNode(message)); | |
top.firstElementChild.insertAdjacentElement("beforebegin", p); | |
}; | |
(async () => { | |
const packagePattern = /https:\/\/www\.npmjs\.com\/package\/([^\/?#&]+)/; | |
try { | |
const match = location.href.match(packagePattern); | |
if (!match) { | |
throw new Error(NAME + "Does not match packageName"); | |
} | |
const [all, packageName] = match; | |
if (!packageName) { | |
throw new Error(NAME + "Not found packageName"); | |
} | |
const pkg = await fetchPackage(packageName).catch(error => { | |
console.log(NAME, "does not `types` field"); | |
}); | |
if (typeof pkg["types"] === "string") { | |
insertToTop("support `types` field in package.json"); | |
return; | |
} | |
if (typeof pkg["typings"] === "string") { | |
insertToTop("support `typings` field in package.json"); | |
return; | |
} | |
const typesPkg = await fetchPackage(`@types/${packageName}`).catch(error => { | |
console.log(NAME, "does not has @types package"); | |
}); | |
if (typeof typesPkg === "object") { | |
insertToTop("support `@types` packages: " + `@types/${packageName}`); | |
return; | |
} | |
throw new Error("Does not support types"); | |
} catch (error) { | |
insertToTop(error.message); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment