// ==UserScript== // @name Start Button for Copying Title and URL // @namespace http://tampermonkey.net/ // @version 1.0 // @description Adds a start button on the left side of the page for copying the title and URL of the webpage and pasting it into a text box on the right side of the page with the option to save the text box contents in various file formats. // @author Your Name // @match :///* // @grant GM_setClipboard // ==/UserScript==

(function() { 'use strict';

// 创建“Start”按钮元素
var startBtn = document.createElement('div');
startBtn.style.position = 'fixed';
startBtn.style.top = '50%';
startBtn.style.left = '20px';
startBtn.style.transform = 'translateY(-50%)';
startBtn.style.width = '50px';
startBtn.style.height = '50px';
startBtn.style.borderRadius = '50%';
startBtn.style.backgroundColor = 'green';
startBtn.style.cursor = 'pointer';
startBtn.style.zIndex = '9999';
startBtn.innerHTML = 'Start';
document.body.appendChild(startBtn);

// 为“Start”按钮添加点击事件监听器
startBtn.addEventListener('click', function() {
    // 获取网页标题和链接
    var title = document.title;
    var url = window.location.href;
    var text = title + ' ' + url;

    // 将文本复制到剪贴板
    GM_setClipboard(text);

    // 创建文本框元素
    var textBox = document.createElement('div');
    textBox.style.position = 'fixed';
    textBox.style.top = '50%';
    textBox.style.right = '20px';
    textBox.style.transform = 'translateY(-50%)';
    textBox.style.width = '300px';
    textBox.style.height = '200px';
    textBox.style.border = '1px solid black';
    textBox.style.padding = '10px';
    textBox.style.overflow = 'auto';
    textBox.style.zIndex = '9999';

    // 创建“刷新”按钮元素
    var refreshBtn = document.createElement('button');
    refreshBtn.innerHTML = 'Refresh';
    textBox.appendChild(refreshBtn);

    // 为“刷新”按钮添加点击事件监听器
    refreshBtn.addEventListener('click', function() {
        // 清空文本框内容
        textBox.innerHTML = '';
    });

    // 创建“保存”按钮元素
    var saveBtn = document.createElement('button');
    saveBtn.innerHTML = 'Save';
    textBox.appendChild(saveBtn);

    // 为“保存”按钮添加点击事件监听器
    saveBtn.addEventListener('click', function() {
        // 将文本框内容转换为 Blob 对象
        var blob = new Blob([textBox.innerHTML], {type: 'text/plain;charset=utf-8'});

        // 创建下载链接元素
        var downloadLink = document.createElement('a');
        downloadLink.innerHTML = 'Download';
        downloadLink.href = URL.createObjectURL(blob);
        downloadLink.download = 'title_and_url.txt';
        textBox.appendChild(downloadLink);

        // 清空文本框内容
        textBox.innerHTML = '';
    });

    // 将文本框元素添加到文档中
    document.body.appendChild(textBox);

    // 为文本框添加粘贴事件监听器
    textBox.addEventListener('paste', function(e) {
        // 获取粘贴的文本
        var clipboardData = e.clipboardData || window.clipboardData;
        var pastedText = clipboardData.getData('text');

        // 将网页链接附加到粘贴的文本
        var newText = pastedText + ' ' + url;

        // 用新文本替换文本框内容
        textBox.innerHTML = newText;
    });
});

})();

油猴脚本:自动复制网页标题和链接并保存到文本框

原文地址: https://www.cveoy.top/t/topic/nsgT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录