复制 Cookie

添加一个按钮点击后复制当前页面的 Cookie 信息到剪切板(兼容写法)

  1. // ==UserScript==
  2. // @name 复制 Cookie
  3. // @namespace http://tampermonkey.net/
  4. // @match https://www.kuafuzys.com/*
  5. // @match https://bbs.52huahua.cc/*
  6. // @match https://www.kuafuzy.com/*
  7. // @version 1.0.1
  8. // @description 添加一个按钮点击后复制当前页面的 Cookie 信息到剪切板(兼容写法)
  9. // @author PYY
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. // 创建按钮
  17. const button = document.createElement('button');
  18. button.textContent = '📋 复制 Cookie';
  19. button.style.position = 'fixed';
  20. button.style.bottom = '20px';
  21. button.style.right = '20px';
  22. button.style.zIndex = '9999';
  23. button.style.padding = '10px 14px';
  24. button.style.backgroundColor = '#4CAF50';
  25. button.style.color = 'white';
  26. button.style.border = 'none';
  27. button.style.borderRadius = '6px';
  28. button.style.cursor = 'pointer';
  29. button.style.boxShadow = '0 2px 6px rgba(0,0,0,0.3)';
  30. button.style.fontSize = '14px';
  31.  
  32. document.body.appendChild(button);
  33.  
  34. button.addEventListener('click', () => {
  35. const cookie = document.cookie;
  36. if (!cookie) {
  37. alert('⚠️ 当前页面没有 Cookie 可复制');
  38. return;
  39. }
  40.  
  41. if (navigator.clipboard && window.isSecureContext) {
  42. navigator.clipboard.writeText(cookie).then(() => {
  43. alert('✅ Cookie 已复制到剪切板!');
  44. console.log('[Cookie]', cookie);
  45. }).catch(err => {
  46. console.error('❌ 复制失败:', err);
  47. alert('❌ 无法复制 Cookie(权限问题)');
  48. });
  49. } else {
  50. // fallback 写法
  51. const textarea = document.createElement('textarea');
  52. textarea.value = cookie;
  53. document.body.appendChild(textarea);
  54. textarea.select();
  55. try {
  56. const success = document.execCommand('copy');
  57. alert(success ? '✅ Cookie 已复制(兼容方式)' : '❌ 复制失败');
  58. } catch (err) {
  59. console.error('复制异常:', err);
  60. alert('❌ 复制失败(异常)');
  61. }
  62. document.body.removeChild(textarea);
  63. }
  64. });
  65. })();