I'm currently working on a TamperMonkey userscript that aims to identify URLs matching a specific pattern, visit these pages, extract relevant information, and then update the link for each URL with the extracted text. I'm facing some challenges with the regex matching/extraction process.
Although I've confirmed that my regex is functioning correctly, it seems to fail when I run the script, and I'm struggling to extract the desired group. Any assistance would be greatly appreciated.
// ==UserScript==
// @name Pull through titles
// @namespace http://tampermonkey.net/
// @version 0.2
// @description ---
// @author You
// @match https://xxx/curriculum-overview/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant none
// ==/UserScript==
(function() {
'use strict';
const regexPattern = /portfolio-item__text-input-wide\" .*value=\"(.+)\"/;
var evidence = "";
var title = "";
//Show descriptors by default
var x = document.getElementsByClassName("js-descriptors");
for (var i = 0, max = x.length; i < max; i++) {
x[i].style.display = "block";
}
//Find all URLs that link to evidence
var urls = document.querySelectorAll('a[href*="portfolio-item/review"]');
//For each URL, visit the page and extract the title of the evidence and update the innerText with that title
for (i = 0; i < urls.length; i++){
const xhr = new XMLHttpRequest();
xhr.open("GET", urls[i], true);
xhr.responseType = "text";
xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200) {
evidence = xhr.responseText;
}
}
};
xhr.send(null);
title = evidence.match(regexPattern); //extract matching regex pattern
alert(title.toString()); //once I know the string is extracted will append to
//urls[i].innerText = urls[i].toString(); //this line tests that the innerText can be changed to the URL; will change to title variable once working
}
})
();