油猴脚本:自动复制网页标题并附加来源链接,带刷新和保存功能
// ==UserScript== // @name Copy and Paste with Source // @namespace http://tampermonkey.net/ // @version 1.0 // @description Copy and paste with source URL attached to copied text // @author Your Name // @match https:/// // @match http:/// // @grant GM_setClipboard // ==/UserScript==
(function() { 'use strict';
// 创建 "Start" 按钮
const startBtn = document.createElement('button');
startBtn.innerText = 'Start';
startBtn.addEventListener('click', () => {
const title = document.querySelector('title').innerText; // 获取 <title> 和 </title> 之间的文本
GM_setClipboard(title); // 将文本复制到剪贴板
const pasteBox = document.createElement('textarea'); // 创建用于粘贴的文本框
pasteBox.style.position = 'fixed';
pasteBox.style.right = '10px';
pasteBox.style.top = '10px';
pasteBox.style.width = '300px';
pasteBox.style.height = '100px';
document.body.appendChild(pasteBox); // 将文本框添加到页面
pasteBox.focus(); // 将焦点设置到文本框,以便粘贴
pasteBox.addEventListener('paste', (event) => {
event.preventDefault();
const clipboardData = event.clipboardData || window.clipboardData;
const pastedText = clipboardData.getData('text');
const sourceUrl = window.location.href; // 获取来源 URL
pasteBox.value = pastedText + '\n\nSource: ' + sourceUrl; // 将来源 URL 附加到粘贴的文本
GM_setClipboard(''); // 粘贴后清除剪贴板
});
const refreshBtn = document.createElement('button'); // 创建 "Refresh" 按钮
refreshBtn.innerText = 'Refresh';
refreshBtn.addEventListener('click', () => {
pasteBox.value = ''; // 清除文本框
GM_setClipboard(''); // 清除剪贴板
});
const saveBtn = document.createElement('button'); // 创建 "Save" 按钮
saveBtn.innerText = 'Save';
saveBtn.addEventListener('click', () => {
const fileData = pasteBox.value;
const fileType = 'text/plain'; // 可根据需要更改文件类型
const fileName = 'pasted-text.txt'; // 可根据需要更改文件名
const fileBlob = new Blob([fileData], {type: fileType});
const fileUrl = URL.createObjectURL(fileBlob);
const downloadLink = document.createElement('a');
downloadLink.href = fileUrl;
downloadLink.download = fileName;
downloadLink.click();
});
document.body.appendChild(refreshBtn); // 将 "Refresh" 按钮添加到页面
document.body.appendChild(saveBtn); // 将 "Save" 按钮添加到页面
});
document.body.appendChild(startBtn); // 将 "Start" 按钮添加到页面
})();
原文地址: https://www.cveoy.top/t/topic/nsgg 著作权归作者所有。请勿转载和采集!