油猴脚本:网页标题提取工具 - 自动复制、粘贴、保存
// ==UserScript== // @name Start Button // @namespace http://tampermonkey.net/ // @version 1.0 // @description Add a start button to select and copy the title of the webpage to clipboard and open a text box to paste it with the source URL attached; also add a refresh button and a save button to clear and save the content in the text box respectively. // @author Your name // @match https:/// // @match http:/// // @grant GM_setClipboard // ==/UserScript==
(function() { 'use strict';
// Create a start button on the left side of the screen
const startBtn = document.createElement('button');
startBtn.style.position = 'fixed';
startBtn.style.left = '10px';
startBtn.style.top = '50%';
startBtn.style.transform = 'translateY(-50%)';
startBtn.style.backgroundColor = 'green';
startBtn.style.borderRadius = '50%';
startBtn.style.width = '50px';
startBtn.style.height = '50px';
startBtn.style.color = 'white';
startBtn.style.fontWeight = 'bold';
startBtn.style.fontSize = '18px';
startBtn.innerText = 'Start';
document.body.appendChild(startBtn);
// Add event listener to start button
startBtn.addEventListener('click', () => {
const title = document.title; // Get the title of the webpage
GM_setClipboard(title); // Copy the title to clipboard
// Create a text box on the right side of the screen
const textBox = document.createElement('textarea');
textBox.style.position = 'fixed';
textBox.style.right = '10px';
textBox.style.top = '50%';
textBox.style.transform = 'translateY(-50%)';
textBox.style.width = '300px';
textBox.style.height = '200px';
textBox.style.fontSize = '16px';
textBox.style.padding = '10px';
document.body.appendChild(textBox);
// Add event listener to text box
textBox.addEventListener('paste', () => {
const sourceUrl = window.location.href; // Get the URL of the webpage
const clipboardData = event.clipboardData || window.clipboardData;
const pastedData = clipboardData.getData('Text'); // Get the pasted text
textBox.value = pastedData + '
Source URL: ' + sourceUrl; // Append the source URL to the pasted text GM_setClipboard(''); // Clear the clipboard after pasting });
// Create a refresh button on the top-right corner of the text box
const refreshBtn = document.createElement('button');
refreshBtn.style.position = 'absolute';
refreshBtn.style.top = '10px';
refreshBtn.style.right = '10px';
refreshBtn.innerText = 'Refresh';
textBox.appendChild(refreshBtn);
// Add event listener to refresh button
refreshBtn.addEventListener('click', () => {
textBox.value = ''; // Clear the content in the text box
GM_setClipboard(''); // Clear the clipboard after refreshing
});
// Create a save button on the bottom-right corner of the text box
const saveBtn = document.createElement('button');
saveBtn.style.position = 'absolute';
saveBtn.style.bottom = '10px';
saveBtn.style.right = '10px';
saveBtn.innerText = 'Save';
textBox.appendChild(saveBtn);
// Add event listener to save button
saveBtn.addEventListener('click', () => {
const blob = new Blob([textBox.value], {type: 'text/plain;charset=utf-8'}); // Create a blob with the text box content
const url = URL.createObjectURL(blob); // Create a URL for the blob
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'webpage_title.txt'; // Set the filename for the saved file
document.body.appendChild(a);
a.click(); // Click the link to download the file
URL.revokeObjectURL(url); // Revoke the URL after downloading
textBox.value = ''; // Clear the content in the text box
GM_setClipboard(''); // Clear the clipboard after saving
});
});
})();
原文地址: https://www.cveoy.top/t/topic/nsgl 著作权归作者所有。请勿转载和采集!