Button to download PDF from iframe's pdfpath parameter

Adds a button to download PDF from iframe's pdfpath parameter

// ==UserScript==
// @name         Button to download PDF from iframe's pdfpath parameter
// @description  Adds a button to download PDF from iframe's pdfpath parameter
// @name:zh-CN   从iframe的pdfpath参数下载PDF的按钮
// @description:zh-CN  添加一个按钮,用于从iframe的pdfpath参数下载PDF
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @author       aspen138
// @match        https://wk.askci.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create a button element
    const button = document.createElement('button');
    button.textContent = 'Download PDF';
    button.style.position = 'fixed';
    button.style.top = '10px';
    button.style.right = '10px';
    button.style.zIndex = '9999';
    button.style.padding = '10px';
    button.style.backgroundColor = '#4CAF50';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';

    // Append button to the page
    document.body.appendChild(button);

    // Add click event listener to the button
    button.addEventListener('click', () => {
        // Find the iframe
        const iframe = document.querySelector('iframe');
        if (!iframe) {
            alert('No iframe found on the page.');
            return;
        }

        // Get iframe src
        const src = iframe.src;
        if (!src) {
            alert('Iframe src is empty.');
            return;
        }

        // Extract query parameters
        const queryString = src.substring(src.indexOf('?') + 1);
        if (!queryString) {
            alert('No query parameters found in iframe src.');
            return;
        }

        // Parse query parameters
        const urlParams = new URLSearchParams(queryString);
        const downloadUrl = urlParams.get('pdfpath');

        if (!downloadUrl) {
            alert('No pdfpath parameter found in iframe src.');
            return;
        }

        // Open the download URL in a new tab
        window.open(downloadUrl, '_blank');
    });
})();