Add links to preview PDFs in Google Docs viewer on the current page
目前為
// ==UserScript==
// @name PDF Preview Links
// @namespace http://cl.thapar.edu/
// @version 0.1
// @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="]');
// 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);
});
})();