Use regex matches instead of replaces

This commit is contained in:
Yarmo Mackenbach 2020-06-28 14:47:22 +02:00
parent 3421695d72
commit 4fd353c840

View file

@ -304,8 +304,9 @@ async function verifyProof(url, fingerprint) {
// HN // HN
if (/^https:\/\/news.ycombinator.com/.test(url)) { if (/^https:\/\/news.ycombinator.com/.test(url)) {
output.type = "hn"; output.type = "hn";
output.display = url.replace(/https:\/\/news.ycombinator.com\/user\?id=/, ""); match = url.match(/https:\/\/news.ycombinator.com\/user\?id=(.*)/);
output.proofUrlFetch = `https://hacker-news.firebaseio.com/v0/user/${output.display}.json`; output.display = match[1];
output.proofUrlFetch = `https://hacker-news.firebaseio.com/v0/user/${match[1]}.json`;
try { try {
response = await fetch(output.proofUrlFetch, { response = await fetch(output.proofUrlFetch, {
headers: { headers: {
@ -329,8 +330,9 @@ async function verifyProof(url, fingerprint) {
// Reddit // Reddit
if (/^https:\/\/www.reddit.com\/user/.test(url)) { if (/^https:\/\/www.reddit.com\/user/.test(url)) {
output.type = "reddit"; output.type = "reddit";
output.display = url.replace(/^https:\/\/www.reddit.com\/user\//, "").replace(/\/comments\/.*/, ""); match = url.match(/https:\/\/www.reddit.com\/user\/(.*)\/comments\/(.*)\/(.*)\//);
output.url = `https://www.reddit.com/user/${output.display}`; output.display = match[1];
output.url = `https://www.reddit.com/user/${match[1]}`;
output.proofUrlFetch = url.replace(/\/[a-zA-Z0-9_]*\/$/, ".json"); output.proofUrlFetch = url.replace(/\/[a-zA-Z0-9_]*\/$/, ".json");
try { try {
response = await fetch(output.proofUrlFetch, { response = await fetch(output.proofUrlFetch, {
@ -358,10 +360,10 @@ async function verifyProof(url, fingerprint) {
// Github // Github
if (/^https:\/\/gist.github.com/.test(url)) { if (/^https:\/\/gist.github.com/.test(url)) {
output.type = "github"; output.type = "github";
output.display = url.replace(/^https:\/\/gist.github.com\//, "").replace(/\/[a-zA-Z0-9]*$/, ""); match = url.match(/https:\/\/gist.github.com\/(.*)\/(.*)/);
output.url = `https://github.com/${output.display}`; output.display = match[1];
let gistId = url.replace(/^https:\/\/gist.github.com\/[a-zA-Z0-9_-]*\//, ""); output.url = `https://github.com/${match[1]}`;
output.proofUrlFetch = `https://api.github.com/gists/${gistId}`; output.proofUrlFetch = `https://api.github.com/gists/${match[2]}`;
try { try {
response = await fetch(output.proofUrlFetch, { response = await fetch(output.proofUrlFetch, {
headers: { headers: {
@ -396,10 +398,11 @@ async function verifyProof(url, fingerprint) {
json = await response.json(); json = await response.json();
if ('attachment' in json) { if ('attachment' in json) {
// Potentially Mastodon // Potentially Mastodon
match = url.match(/https:\/\/(.*)\/@(.*)/);
json.attachment.forEach((item, i) => { json.attachment.forEach((item, i) => {
if (item.value === fingerprint) { if (item.value === fingerprint) {
output.type = "mastodon"; output.type = "mastodon";
output.display = json.url; output.display = `@${match[2]}@${[match[1]]}`;
output.proofUrlFetch = json.url; output.proofUrlFetch = json.url;
output.isVerified = true; output.isVerified = true;
} }