PDF Preview Links

Add links to preview PDFs in Google Docs viewer on the current page

目前為 2024-03-11 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         PDF Preview Links
// @namespace    http://cl.thapar.edu/
// @version      0.2
// @description  Add links to preview PDFs in Google Docs viewer on the current page
// @author       You
// @match        https://cl.thapar.edu/*.php
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Find all <td> elements with PDF download links
    var targetElements = document.querySelectorAll('td a[href^="download.php?id="]');

    // Function to open all preview links
    function openAllPreviews() {
        // Loop through each matching element
        targetElements.forEach(function(targetTd) {
            // Extract the PDF download link from the existing <a> element
            var pdfDownloadLink = targetTd.getAttribute('href');

            // Construct the Google Docs preview link
            var googleDocsPreviewLink = 'https://docs.google.com/viewer?url=' + encodeURIComponent("https://cl.thapar.edu/" + pdfDownloadLink);

            // Open the preview link in a new tab
            window.open(googleDocsPreviewLink, '_blank');
        });
    }

    // Loop through each matching element
    targetElements.forEach(function(targetTd) {
        // Extract the PDF download link from the existing <a> element
        var pdfDownloadLink = targetTd.getAttribute('href');

        // Construct the Google Docs preview link
        var googleDocsPreviewLink = 'https://docs.google.com/viewer?url=' + encodeURIComponent("https://cl.thapar.edu/" + pdfDownloadLink);

        // Create a new link for the preview with a custom class
        var previewLink = document.createElement('a');
        previewLink.href = googleDocsPreviewLink;
        previewLink.target = '_blank';
        previewLink.textContent = 'Preview';
        previewLink.className = 'pdf-preview-link';

        // Append the new preview link to the target <td> element
        targetTd.appendChild(document.createTextNode(' | ')); // Separator
        targetTd.appendChild(previewLink);
    });

    // Create a button to preview all files at once
    var previewAllButton = document.createElement('button');
    previewAllButton.textContent = 'Preview All';
    previewAllButton.addEventListener('click', openAllPreviews);

    // Add the button to the page (you can customize the target element)
    document.body.appendChild(previewAllButton);
})();