copyCookie

一键复制网站的cookie,支持字符串,json。

  1. // ==UserScript==
  2. // @name copyCookie
  3. // @namespace copyCookie
  4. // @version 0.1.2
  5. // @author everstu
  6. // @description 一键复制网站的cookie,支持字符串,json。
  7. // @grant GM_registerMenuCommand
  8. // @grant GM_setClipboard
  9. // @match *://*/*
  10. // @exclude *://192.*
  11. // @run-at document-end
  12. // @license MIT
  13. // @require https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.5.1/jquery.min.js
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. "use strict";
  18.  
  19. function MyTools() {
  20. this.cjVersion = '0.1.0';
  21. this.htmlCode = {
  22. show: "<div id=\"show\" style=\"position: fixed;z-index:9999;bottom:55px;right:1px;background: #000000;color: white;border-radius: 17px;width: 30px;height: 45px;text-align:center;cursor: pointer;line-height: 45px;font-size:14px;opacity: 0.3;\" >显</div>",
  23. hidden: "<div id=\"hidden\" style=\"position: fixed;z-index:9999;bottom:190px;right:1px;background: #000000;color: white;border-radius: 17px;width: 120px;height: 40px;text-align:center;cursor: pointer;line-height: 40px;font-size:14px;opacity: 0.6;\">隐藏插件</div>",
  24. cjversion: "<div style=\"position: fixed;z-index:9999;bottom:145px;right:1px;background: #000000;color: white;border-radius: 17px;width: 120px;height: 40px;text-align:center;line-height: 20px;font-size:12px;opacity: 0.6;\">copyCookie<br/>当前版本:" + this.cjVersion + "</div>",
  25. copycookie: "<div id=\"copycookie\" style=\"position: fixed;z-index:9999;bottom:100px;right:1px;background: #CD0B02;color: white;border-radius: 17px;width: 120px;height: 40px;cursor: pointer;text-align: center;line-height: 40px;font-size:16px;opacity: 0.9;\" data-can='yes'>复制成文本</div>",
  26. copycookiejson: "<div id=\"copycookiejson\" style=\"position: fixed;z-index:9999;bottom:55px;right:1px;background: #CD0B02;color: white;border-radius: 17px;width: 120px;height: 40px;cursor: pointer;text-align: center;line-height: 40px;font-size:15px;opacity: 0.9;\" data-can='yes'>复制成JSON</div>",
  27. d_container_start: "<div id=\"d_container\" style=\"display:none;width: 120px;height: 185px;position: fixed;bottom: 1px;right:1px;\">",
  28. d_container_end: "</div>",
  29. };
  30. }
  31.  
  32. MyTools.prototype.getCookiesStr = function () {
  33. return document.cookie;
  34. };
  35.  
  36. MyTools.prototype.getCookieObj = function () {
  37. let cookieObj = {};
  38. let cookieStr = this.getCookiesStr();
  39. let pairList = cookieStr.split(';');
  40. for (var _i = 0, pairList_1 = pairList; _i < pairList_1.length; _i++) {
  41. let pair = pairList_1[_i];
  42. let _a = pair.trim().split('='), key = _a[0], value = _a[1];
  43. cookieObj[key] = value;
  44. }
  45. return cookieObj;
  46. };
  47.  
  48. MyTools.prototype.getCookiesJson = function () {
  49. let cookieObj = this.getCookieObj();
  50. return JSON.stringify(cookieObj);
  51. };
  52.  
  53. MyTools.prototype.copyCookieJson = function () {
  54. let cookieJson = this.getCookiesJson();
  55. GM_setClipboard(cookieJson, {type: 'text', mimetype: 'text/plain'});
  56. };
  57.  
  58. MyTools.prototype.copyCookieString = function () {
  59. GM_setClipboard(this.getCookiesStr(), {type: 'text', mimetype: 'text/plain'});
  60. };
  61.  
  62. MyTools.prototype.copyString = function (type, obj) {
  63. let oldHtml = '';
  64. let domObj = $(obj);
  65. if (domObj.data('can') === 'no') {
  66. return;
  67. }
  68. if (type === 'json') {
  69. this.copyCookieJson();
  70. oldHtml = '复制成JSON';
  71. } else {
  72. this.copyCookieString();
  73. oldHtml = '复制成文本';
  74. }
  75. domObj.data('can', 'no');
  76. domObj.css('background', '#0C986C');
  77. domObj.html('复制成功');
  78. setTimeout(function () {
  79. domObj.data('can', 'yes');
  80. domObj.html(oldHtml);
  81. domObj.css('background', '#CD0B02');
  82. }, 300);
  83. };
  84.  
  85. MyTools.prototype.initTools = function () {
  86. let html = '';
  87. let obj = this;
  88. html += this.htmlCode.show;
  89. html += this.htmlCode.d_container_start;
  90. // html += this.htmlCode.hidden;
  91. html += this.htmlCode.cjversion;
  92. html += this.htmlCode.copycookie;
  93. html += this.htmlCode.copycookiejson;
  94. html += this.htmlCode.d_container_end;
  95. $('body').append(html);
  96.  
  97. $('#copycookie').click(function () {
  98. obj.copyString('string', this);
  99. });
  100.  
  101. $('#copycookiejson').click(function () {
  102. obj.copyString('json', this);
  103. });
  104.  
  105. $('#d_container').mouseleave(function () {
  106. $(this).hide();
  107. $('#show').show();
  108. });
  109.  
  110. $('#show').hover(function () {
  111. $(this).hide();
  112. $('#d_container').show();
  113. });
  114. };
  115.  
  116. MyTools.prototype.request = function () {
  117. $.ajax({
  118. type: "get",
  119. async: false,
  120. url: '',
  121. dataType: "text",
  122. xhrFields: {
  123. withCredentials: true
  124. },
  125. success: function (res) {
  126. console.log(this);
  127. },
  128. error: function (res) {
  129. }
  130. });
  131. }
  132.  
  133.  
  134. setTimeout(function () {
  135. let tools = new MyTools();
  136. tools.initTools();
  137. }, 100);
  138. })();