PDF Preview Links

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

当前为 2024-03-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();