logoExportScript

Find the svg tag in your website and add an export button!

目前為 2023-09-07 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name logoExportScript
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Find the svg tag in your website and add an export button!
  6. // @author Hubery
  7. // @match *://www.logosc.cn/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @require http://code.jquery.com/jquery-2.1.1.min.js
  10. // @require https://cdn.bootcdn.net/ajax/libs/canvasjs/1.7.0/canvasjs.min.js
  11. // @grant GM_download
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. // 导出SVG为文件
  16. function exportSVG(svgContent, fileName) {
  17. var blob = new Blob([svgContent], { type: 'image/svg+xml' });
  18. var url = URL.createObjectURL(blob);
  19. var link = document.createElement('a');
  20. link.href = url;
  21. link.download = fileName;
  22. link.click();
  23. URL.revokeObjectURL(url);
  24. }
  25. // 导出为png
  26. function exportSVGToPNG(svgElement, scaleFactor=2) {
  27. var width = svgElement.clientWidth * scaleFactor;
  28. var height = svgElement.clientHeight * scaleFactor;
  29. // 绘制 SVG 到画布
  30. var svgData = new XMLSerializer().serializeToString(svgElement);
  31. // 创建一个新的Image对象
  32. var image = new Image();
  33. image.src = 'data:image/svg+xml;base64,' + btoa(svgData);
  34. // 当图像加载完成时
  35. image.onload = function() {
  36. var canvas = document.createElement('canvas');
  37. canvas.width = width;
  38. canvas.height = height;
  39. var context = canvas.getContext('2d');
  40.  
  41. // 在画布上绘制图像
  42. context.drawImage(image, 0, 0);
  43.  
  44. // 导出画布为PNG并下载
  45. GM_download(canvas.toDataURL('image/png'), 'export.png');
  46. };
  47.  
  48. }
  49.  
  50. function identifyDivs($) {
  51. $('.card.azoomIn').each(function() {
  52. var $div = $(this);
  53. if (!$div.attr('selected')) {
  54. // 做一些你想要的操作,例如添加selected属性后的样式修改
  55. $div.attr('selected', 'true');
  56. // 添加按钮
  57. var $svgButton = $('<button>').text('下载svg');
  58. $svgButton.css({
  59. color: 'white',
  60. backgroundColor: '#1677ff',
  61. fontSize: '14px',
  62. height: '32px',
  63. padding: '4px 15px',
  64. borderRadius: '6px',
  65. border: 'none',
  66. 'margin-left': '10px',
  67. });
  68.  
  69. // 添加点击事件处理程序
  70. $svgButton.click(function() {
  71. console.log($(this).text()); // 打印当前按钮的内容
  72. var $svg = $div.find('.svg-card svg');
  73. if ($svg.length > 0) {
  74. var svgContent = $svg[0].outerHTML
  75. exportSVG(svgContent, 'svg_file.svg');
  76. }
  77. });
  78. $div.prepend($svgButton);
  79.  
  80.  
  81. var $pngButton = $('<button>').text('下载png');
  82. $pngButton.css({
  83. color: 'white',
  84. backgroundColor: '#48ce15',
  85. fontSize: '14px',
  86. height: '32px',
  87. padding: '4px 15px',
  88. borderRadius: '6px',
  89. border: 'none'
  90. });
  91. // 添加点击事件处理程序
  92. $pngButton.click(function() {
  93. var $svg = $div.find('.svg-card svg');
  94. if ($svg.length > 0) {
  95. // var svgContent = $svg[0].outerHTML
  96. var svgElement = $svg[0]
  97. var scaleFactor = $("#scaleFactor").val()
  98. exportSVGToPNG(svgElement, scaleFactor);
  99. }
  100. });
  101. $div.prepend($pngButton);
  102. }
  103. });
  104. }
  105.  
  106. function showDownloadButton($){
  107. var button = $('<button>显示下载</button>');
  108. // 设置按钮的 CSS 样式
  109. button.css({
  110. color: 'white',
  111. backgroundColor: '#16baaa',
  112. fontSize: '14px',
  113. position: 'fixed',
  114. top: '55%',
  115. left: '10px',
  116. height: '32px',
  117. borderRadius: '6px',
  118. border: 'none',
  119. padding: '4px 15px',
  120. 'transform': 'translateY(-50%)',
  121. });
  122. button.click(function(){
  123. identifyDivs($)
  124. })
  125. $('body').append(button);
  126. //添加png比例
  127. var inputDiv =$('<div class="InputBox"><label for="factor">png倍数:</label><input type="number" value=1 id="scaleFactor" style="height: 22px;width: 55px; line-height: 1.3rem; line-height: 22px; border-width: 1px; border-style: solid; background-color: #fff; color: rgba(0,0,0,.85); border-radius: 2px;" step="0.1"></div>')
  128. inputDiv.css({
  129. fontSize: '14px',
  130. position: 'fixed',
  131. top: '60%',
  132. left: '10px'
  133. })
  134. // 将按钮添加到 body 元素中
  135. $('body').append(inputDiv);
  136. }
  137.  
  138. function removeWatermarklayer($) {
  139. // 创建按钮元素
  140. var button = $('<button>去水印</button>');
  141.  
  142. // 设置按钮的 CSS 样式
  143. button.css({
  144. color: 'white',
  145. backgroundColor: '#16baaa',
  146. fontSize: '14px',
  147. position: 'fixed',
  148. top: '50%',
  149. left: '10px',
  150. height: '32px',
  151. borderRadius: '6px',
  152. border: 'none',
  153. padding: '4px 15px',
  154. 'transform': 'translateY(-50%)',
  155. });
  156. button.click(function(){
  157. $(".watermarklayer").remove()
  158. })
  159. // 将按钮添加到 body 元素中
  160. $('body').append(button);
  161. }
  162.  
  163. (function($) {
  164. 'use strict';
  165. removeWatermarklayer($);
  166. showDownloadButton($);
  167. // setInterval(function() {identifyDivs(jQuery);}, 2000);
  168. // Your code here...
  169. })(jQuery);