短链生成

点击"生成短链"后发送POST请求并解析返回的JSON数据

  1. // ==UserScript==
  2. // @name 短链生成
  3. // @namespace http://d.glf2ym.cn/
  4. // @version 0.21
  5. // @description 点击"生成短链"后发送POST请求并解析返回的JSON数据
  6. // @author You
  7. // @match *://*/*
  8. // @grant GM_setClipboard
  9. // @grant GM_registerMenuCommand
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function addElement({
  16. tag,
  17. attrs = {},
  18. to = document.body || document.documentElement,
  19. }) {
  20. const el = document.createElement(tag);
  21. Object.assign(el, attrs);
  22. to.appendChild(el);
  23. return el;
  24. }
  25.  
  26. function addStyle(css) {
  27. return addElement({
  28. tag: 'style',
  29. attrs: {
  30. textContent: css,
  31. },
  32. to: document.head,
  33. });
  34. }
  35.  
  36. var config = {
  37. "toast": 0.1,
  38. "out": 1
  39. };
  40.  
  41. function toast(text, time = 3, callback, transition = 0.2) {
  42. let isObj = (o) => typeof o == 'object' && typeof o.toString == 'function' && o.toString() === '[object Object]', timeout, toastTransCount = 0;
  43. if (typeof text != 'string') text = String(text);
  44. if (typeof time != 'number' || time <= 0) time = 3;
  45. if (typeof transition != 'number' || transition < 0) transition = 0.2;
  46. if (callback && !isObj(callback)) callback = undefined;
  47. if (callback) {
  48. if (callback.text && typeof callback.text != 'string') callback.text = String(callback.text);
  49. if (
  50. callback.color && (typeof callback.color != 'string' || callback.color === '')) delete callback.color;
  51. if (callback.onclick && typeof callback.onclick != 'function') callback.onclick = () => null;
  52. if (callback.onclose && typeof callback.onclose != 'function') delete callback.onclose;
  53. }
  54.  
  55. let toastStyle = addStyle(`
  56. #bextToast {
  57. all: initial;
  58. display: flex;
  59. position: fixed;
  60. left: 0;
  61. right: 0;
  62. bottom: 10vh;
  63. width: max-content;
  64. max-width: 80vw;
  65. max-height: 80vh;
  66. margin: 0 auto;
  67. border-radius: 20px;
  68. padding: .5em 1em;
  69. font-size: 16px;
  70. background-color: rgba(0,0,0,0.5);
  71. color: white;
  72. z-index: 1000002;
  73. opacity: 0%;
  74. transition: opacity ${transition}s;
  75. }
  76. #bextToast > * {
  77. display: -webkit-box;
  78. height: max-content;
  79. margin: auto .25em;
  80. width: max-content;
  81. max-width: calc(40vw - .5em);
  82. max-height: 80vh;
  83. overflow: hidden;
  84. -webkit-line-clamp: 22;
  85. -webkit-box-orient: vertical;
  86. text-overflow: ellipsis;
  87. overflow-wrap: anywhere;
  88. }
  89. #bextToastBtn {
  90. color: ${callback && callback.color ? callback.color : 'turquoise'}
  91. }
  92. #bextToast.bextToastShow {
  93. opacity: 1;
  94. }
  95. `),
  96. toastDiv = addElement({
  97. tag: 'div',
  98. attrs: {
  99. id: 'bextToast',
  100. },
  101. }),
  102. toastShow = () => {
  103. toastDiv.classList.toggle('bextToastShow');
  104. toastTransCount++;
  105. if (toastTransCount >= 2) {
  106. setTimeout(function() {
  107. toastDiv.remove();
  108. toastStyle.remove();
  109. if (callback && callback.onclose) callback.onclose.call(this);
  110. }, transition * 1000 + 1);
  111. }
  112. };
  113. addElement({
  114. tag: 'div',
  115. attrs: {
  116. id: 'bextToastText',
  117. innerText: text,
  118. },
  119. to: toastDiv,
  120. });
  121. if (callback && callback.text) {
  122. addElement({
  123. tag: 'div',
  124. attrs: {
  125. id: 'bextToastBtn',
  126. innerText: callback.text,
  127. onclick: callback && callback.onclick ? () => {
  128. callback.onclick.call(this);
  129. clearTimeout(timeout);
  130. toastShow();
  131. } : null,
  132. },
  133. to: toastDiv,
  134. });
  135. }
  136. setTimeout(toastShow, 1);
  137. timeout = setTimeout(toastShow, (time + transition * 2) * 1000);
  138. }
  139.  
  140. GM_registerMenuCommand("3M短网址", function() {
  141. // 获取当前页面的网址
  142. var currentPageURL = window.location.href;
  143. threeM(currentPageURL);
  144. });
  145. GM_registerMenuCommand("TK短网址", function() {
  146. // 获取当前页面的网址
  147. var currentPageURL = window.location.href;
  148. TK(currentPageURL);
  149. });
  150.  
  151. function threeM(currentPageURL) {
  152. fetch("https://3mw.cn/api/url/shorten/", {
  153. method: "POST",
  154. headers: {
  155. Host: "3mw.cn",
  156. "Content-Length": "17",
  157. Origin: "https://uutool.cn",
  158. Referer: "https://uutool.cn/",
  159. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  160. },
  161. body: "url=" + encodeURIComponent(currentPageURL)
  162. })
  163. .then(response => {
  164. if (!response.ok) {
  165. toast("json获取失败");
  166. }
  167. return response.json();
  168. })
  169. .then(data => {
  170. if(data.status === 1) {
  171. var shorten_url = data.data.shorten_url;
  172. GM_setClipboard(shorten_url);
  173. toast("已复制:"+shorten_url, 2);
  174. } else {
  175. toast(data.error);
  176. }
  177. })
  178. .catch(error => {
  179. toast("缩短失败,请再次尝试,如仍然失败请携带网址反馈");
  180. });
  181. }
  182.  
  183. function TK(currentPageURL) {
  184. // 发送POST请求
  185. fetch('https://d.glf2ym.cn', {
  186. method: 'POST',
  187. headers: {
  188. 'Host': 'd.glf2ym.cn',
  189. 'Content-Type': 'application/json',
  190. 'Origin': 'http://d.glf2ym.cn',
  191. 'Referer': 'http://d.glf2ym.cn/'
  192. },
  193. body: JSON.stringify({ url: currentPageURL })
  194. })
  195. .then(response => {
  196. if (!response.ok) {
  197. toast("json获取失败");
  198. }
  199. return response.json();
  200. })
  201. .then(data => {
  202. if (data.status === 200) {
  203. var shortenedURL = "https://d.glf2ym.cn" + data.key;
  204. GM_setClipboard(shortenedURL);
  205. toast("已复制:" + shortenedURL, 2);
  206. } else if (data.status === 500) {
  207. toast(data.key.replace(": Error:", ""), 2);
  208. threeM(currentPageURL);
  209. } else {
  210. toast(data.status+":缩短失败");
  211. }
  212. })
  213. .catch(error => {
  214. toast("缩短失败,请再次尝试,如仍然失败请携带网址反馈");
  215. });
  216. }
  217.  
  218. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址