Greasy Fork 支持简体中文。

Legal Acts Finder

Find legal acts and rules mentioned on webpages and provide links to their PDF documents.

// ==UserScript==
// @name         Legal Acts Finder
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Find legal acts and rules mentioned on webpages and provide links to their PDF documents.
// @author       iamnobody
// @license      MIT
// @icon         https://greasyfork.s3.us-east-2.amazonaws.com/90lf1h9eegn8gwjxurg9x8y0gsg7
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Define regular expressions to match legal acts and rules in various formats
    const actRegex = /(\w+\s+act\s+of\s+\d{4}\s+\w+)|(\w+\s+act,\s+\d{4})|(\w+\s+act\s+of\s+year\s+\d{4})/gi;
    const ruleRegex = /section\s+\w+\s+of\s+\w+\s+act,\s+\d{4}/gi;

    // Define rate limiting parameters
    const REQUEST_INTERVAL = 60000; // Request interval in milliseconds (e.g., 2000 ms = 2 seconds)
    let lastRequestTime = 0; // Initialize the last request time

    // Function to extract and display legal acts and rules
    function findAndDisplayLegalActs() {
        // Check if enough time has passed since the last request
        const currentTime = Date.now();
        const elapsedTime = currentTime - lastRequestTime;
        if (elapsedTime < REQUEST_INTERVAL) {
            // If not enough time has passed, wait before making the request
            setTimeout(findAndDisplayLegalActs, REQUEST_INTERVAL - elapsedTime);
            return;
        }

        const textContent = document.body.textContent;

        // Find legal acts
        const actsMatches = textContent.match(actRegex) || [];

        // Find legal rules
        const rulesMatches = textContent.match(ruleRegex) || [];

        // Combine matches
        const allMatches = actsMatches.concat(rulesMatches);

        if (allMatches.length > 0) {
            // Create a collapsible panel for displaying legal acts and rules
            const panel = document.createElement('div');
            panel.setAttribute('id', 'legal-acts-panel');
            panel.style.position = 'fixed';
            panel.style.top = '50%';
            panel.style.right = '0';
            panel.style.transform = 'translateY(-50%)';
            panel.style.padding = '10px';
            panel.style.backgroundColor = 'orange';
            panel.style.color = 'white';
            panel.style.borderTopLeftRadius = '5px';
            panel.style.borderBottomLeftRadius = '5px';
            panel.style.cursor = 'pointer';
            panel.textContent = '>';

            // Function to toggle the panel
            function togglePanel() {
                // Toggle panel content
            }

            panel.addEventListener('click', togglePanel);

            // Append the panel to the document
            document.body.appendChild(panel);

            // Update last request time
            lastRequestTime = Date.now();
        }
    }

    // Run the function when the page is fully loaded
    window.addEventListener('load', findAndDisplayLegalActs);
})();

(function() {
    'use strict';

    // Define regular expressions to match legal acts and rules in various formats
    const actRegex = /(\w+\s+act\s+of\s+\d{4}\s+\w+)|(\w+\s+act,\s+\d{4})|(\w+\s+act\s+of\s+year\s+\d{4})/gi;
    const ruleRegex = /section\s+\w+\s+of\s+\w+\s+act,\s+\d{4}/gi;

    // Function to extract and display legal acts and rules
    function findAndDisplayLegalActs() {
        const textContent = document.body.textContent;

        // Find legal acts
        const actsMatches = textContent.match(actRegex) || [];

        // Find legal rules
        const rulesMatches = textContent.match(ruleRegex) || [];

        // Combine matches
        const allMatches = actsMatches.concat(rulesMatches);

        if (allMatches.length > 0) {
            // Create a collapsible panel for displaying legal acts and rules
            const panel = document.createElement('div');
            panel.setAttribute('id', 'legal-acts-panel');
            panel.style.position = 'fixed';
            panel.style.top = '50%';
            panel.style.right = '0';
            panel.style.transform = 'translateY(-50%)';
            panel.style.padding = '10px';
            panel.style.backgroundColor = 'orange';
            panel.style.color = 'white';
            panel.style.borderTopLeftRadius = '5px';
            panel.style.borderBottomLeftRadius = '5px';
            panel.style.cursor = 'pointer';
            panel.textContent = '<'; // Default to collapsed state

            // Function to toggle the panel
            function togglePanel() {
                if (panel.textContent === '>') {
                    panel.textContent = '<';
                    panel.style.paddingLeft = '20px';
                    panel.style.paddingRight = '10px';
                    panel.style.backgroundColor = 'orange';
                    panel.style.color = 'white';
                    panel.style.borderTopRightRadius = '5px';
                    panel.style.borderBottomRightRadius = '5px';
                    panel.style.borderTopLeftRadius = '0';
                    panel.style.borderBottomLeftRadius = '0';

                    // Remove the list if it exists
                    if (panel.lastChild) {
                        panel.removeChild(panel.lastChild);
                    }
                } else {
                    panel.textContent = '>';
                    panel.style.paddingLeft = '10px';
                    panel.style.paddingRight = '10px';
                    panel.style.backgroundColor = 'white';
                    panel.style.color = 'black';
                    panel.style.borderTopRightRadius = '0';
                    panel.style.borderBottomRightRadius = '0';
                    panel.style.borderTopLeftRadius = '5px';
                    panel.style.borderBottomLeftRadius = '5px';

                    // Create a list to display legal acts and rules
                    const list = document.createElement('ul');
                    list.style.listStyle = 'none';
                    list.style.margin = '0';
                    list.style.padding = '0';
                    list.style.maxHeight = '200px';
                    list.style.overflowY = 'auto';

                    // Function to add an item to the list
                    function addItem(text, pdfUrl) {
                        const item = document.createElement('li');
                        const link = document.createElement('a');
                        link.textContent = text;
                        link.href = pdfUrl;
                        link.target = '_blank'; // Open PDF in a new tab
                        link.style.textDecoration = 'none';
                        link.style.color = 'inherit';
                        link.style.display = 'block';
                        link.style.padding = '5px';
                        link.style.borderBottom = '1px solid #ccc';
                        link.style.transition = 'background-color 0.3s';
                        link.addEventListener('mouseover', function() {
                            link.style.backgroundColor = '#f0f0f0';
                        });
                        link.addEventListener('mouseout', function() {
                            link.style.backgroundColor = 'transparent';
                        });
                        item.appendChild(link);
                        list.appendChild(item);
                    }

                    // Add legal acts and rules to the list
                    allMatches.forEach(match => {
                        // Fetch PDF links for each mentioned act or rule
                        fetchPDFLink(match).then(pdfUrl => {
                            addItem(match, pdfUrl);
                        });
                    });

                    panel.appendChild(list);
                }
            }

            panel.addEventListener('click', togglePanel);

            // Append the panel to the document
            document.body.appendChild(panel);
        }
    }
    // Function to fetch PDF links for legal acts or rules
    function fetchPDFLink(query) {
        return new Promise((resolve, reject) => {
            // Construct Google search URL
            const googleSearchUrl = `https://www.google.com/search?q=${encodeURIComponent(query + ' pdf')}`;

            // Send a GET request to the Google search URL
            GM_xmlhttpRequest({
                method: 'GET',
                url: googleSearchUrl,
                onload: function(response) {
                    // Extract the first search result URL
                    const match = response.responseText.match(/"https?:\/\/[^"]*\.pdf"/);
                    if (match) {
                        // Resolve with the PDF URL
                        resolve(match[0].replace(/"/g, ''));
                    } else {
                        // If no PDF link is found, resolve with a Google search link
                        resolve(googleSearchUrl);
                    }
                },
                onerror: function(error) {
                    // Handle errors
                    reject(error);
                }
            });
        });
    }

    // Run the function when the page is fully loaded
    window.addEventListener('load', findAndDisplayLegalActs);
})();