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.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);
})();