// ==UserScript==
// @name HTML Source Copier
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 为网页添加按钮以复制完整HTML源码
// @author YourName
// @match *://*/*
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
// 创建按钮样式
const style = document.createElement('style');
style.textContent = `
.html-copy-btn {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9999;
padding: 10px 15px;
background: linear-gradient(135deg, #6e8efb, #a777e3);
color: white;
border: none;
border-radius: 50px;
cursor: pointer;
font-weight: bold;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
transition: all 0.3s ease;
}
.html-copy-btn:hover {
transform: translateY(-3px);
box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}
.html-copy-btn:active {
transform: translateY(1px);
}
.copy-notification {
position: fixed;
bottom: 70px;
right: 20px;
background: rgba(0,0,0,0.7);
color: white;
padding: 10px 15px;
border-radius: 5px;
opacity: 0;
transition: opacity 0.3s;
}
`;
document.head.appendChild(style);
// 创建按钮元素
const btn = document.createElement('button');
btn.className = 'html-copy-btn';
btn.textContent = 'Copy HTML';
document.body.appendChild(btn);
// 创建通知元素
const notification = document.createElement('div');
notification.className = 'copy-notification';
document.body.appendChild(notification);
// 按钮点击事件
btn.addEventListener('click', function() {
const html = document.documentElement.outerHTML;
GM_setClipboard(html, 'text')
.then(() => {
notification.textContent = 'HTML copied to clipboard!';
notification.style.opacity = '1';
setTimeout(() => {
notification.style.opacity = '0';
}, 2000);
})
.catch(err => {
notification.textContent = 'Copy failed: ' + err;
notification.style.opacity = '1';
setTimeout(() => {
notification.style.opacity = '0';
}, 3000);
});
});
})();