Extracting and Exporting data to a csv file using javascript

I’m trying to extract data or number from a webpage and export it to a csv file by a push of a button. Here is the code I’m using but it doesn’t do anything when you click the button. Hoping that someone can help me resolve the issue if this is possible.

Kindly change the webpage as I have tested it with mine. Thank you very much in advance.

// ==UserScript==
// @name         Extract and Export Data
// @namespace    http://your-website.com
// @version      1.0
// @description  Extract data and export to CSV
// @author       Your Name
// @match        *://*/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

// Function to extract the number before "Data1 and Data2"
function extractData() {
    const textContent = document.body.textContent || document.body.innerText;
    const match = /(d+)s*Data1s*ands*Data2/i.exec(textContent);
    const extractedNumber = match && match[1] ? match[1] : '';

    return extractedNumber;
}

// Function to export data to CSV
function exportToCSV(data) {
    const csvContent = 'data:text/csv;charset=utf-8,' + encodeURIComponent(data);
    const link = document.createElement('a');
    link.href = csvContent;
    link.target = '_blank';
    link.download = 'exported_data.csv';
    link.click();
}

// Function to handle the button click
function handleClick() {
    const extractedNumber = extractData();

    if (extractedNumber) {
        // Example: You can customize the CSV format based on your needs
        const csvData = `Number Before Data1 and Data2n${extractedNumber}`;

        // Trigger the CSV export
        exportToCSV(csvData);
    } else {
        console.error('Data extraction failed. Check the page structure.');
    }
}

// Create a button and append it to the page
const button = document.createElement('button');
button.textContent = 'Export Data to CSV';
button.addEventListener('click', handleClick);

// You can customize the button's style here
GM_addStyle(`
    button {
        background-color: #4CAF50;
        color: white;
        padding: 10px;
        font-size: 16px;
        cursor: pointer;
    }
`);

// Append the button to the body of the webpage
document.body.appendChild(button);
})();