- // ==UserScript==
- // @name 短链生成
- // @namespace http://d.glf2ym.cn/
- // @version 0.21
- // @description 点击"生成短链"后发送POST请求并解析返回的JSON数据
- // @author You
- // @match *://*/*
- // @grant GM_setClipboard
- // @grant GM_registerMenuCommand
- // ==/UserScript==
-
- (function() {
- 'use strict';
-
- function addElement({
- tag,
- attrs = {},
- to = document.body || document.documentElement,
- }) {
- const el = document.createElement(tag);
- Object.assign(el, attrs);
- to.appendChild(el);
- return el;
- }
-
- function addStyle(css) {
- return addElement({
- tag: 'style',
- attrs: {
- textContent: css,
- },
- to: document.head,
- });
- }
-
- var config = {
- "toast": 0.1,
- "out": 1
- };
-
- function toast(text, time = 3, callback, transition = 0.2) {
- let isObj = (o) => typeof o == 'object' && typeof o.toString == 'function' && o.toString() === '[object Object]', timeout, toastTransCount = 0;
- if (typeof text != 'string') text = String(text);
- if (typeof time != 'number' || time <= 0) time = 3;
- if (typeof transition != 'number' || transition < 0) transition = 0.2;
- if (callback && !isObj(callback)) callback = undefined;
- if (callback) {
- if (callback.text && typeof callback.text != 'string') callback.text = String(callback.text);
- if (
- callback.color && (typeof callback.color != 'string' || callback.color === '')) delete callback.color;
- if (callback.onclick && typeof callback.onclick != 'function') callback.onclick = () => null;
- if (callback.onclose && typeof callback.onclose != 'function') delete callback.onclose;
- }
-
- let toastStyle = addStyle(`
- #bextToast {
- all: initial;
- display: flex;
- position: fixed;
- left: 0;
- right: 0;
- bottom: 10vh;
- width: max-content;
- max-width: 80vw;
- max-height: 80vh;
- margin: 0 auto;
- border-radius: 20px;
- padding: .5em 1em;
- font-size: 16px;
- background-color: rgba(0,0,0,0.5);
- color: white;
- z-index: 1000002;
- opacity: 0%;
- transition: opacity ${transition}s;
- }
- #bextToast > * {
- display: -webkit-box;
- height: max-content;
- margin: auto .25em;
- width: max-content;
- max-width: calc(40vw - .5em);
- max-height: 80vh;
- overflow: hidden;
- -webkit-line-clamp: 22;
- -webkit-box-orient: vertical;
- text-overflow: ellipsis;
- overflow-wrap: anywhere;
- }
- #bextToastBtn {
- color: ${callback && callback.color ? callback.color : 'turquoise'}
- }
- #bextToast.bextToastShow {
- opacity: 1;
- }
- `),
- toastDiv = addElement({
- tag: 'div',
- attrs: {
- id: 'bextToast',
- },
- }),
- toastShow = () => {
- toastDiv.classList.toggle('bextToastShow');
- toastTransCount++;
- if (toastTransCount >= 2) {
- setTimeout(function() {
- toastDiv.remove();
- toastStyle.remove();
- if (callback && callback.onclose) callback.onclose.call(this);
- }, transition * 1000 + 1);
- }
- };
- addElement({
- tag: 'div',
- attrs: {
- id: 'bextToastText',
- innerText: text,
- },
- to: toastDiv,
- });
- if (callback && callback.text) {
- addElement({
- tag: 'div',
- attrs: {
- id: 'bextToastBtn',
- innerText: callback.text,
- onclick: callback && callback.onclick ? () => {
- callback.onclick.call(this);
- clearTimeout(timeout);
- toastShow();
- } : null,
- },
- to: toastDiv,
- });
- }
- setTimeout(toastShow, 1);
- timeout = setTimeout(toastShow, (time + transition * 2) * 1000);
- }
-
- GM_registerMenuCommand("3M短网址", function() {
- // 获取当前页面的网址
- var currentPageURL = window.location.href;
- threeM(currentPageURL);
- });
- GM_registerMenuCommand("TK短网址", function() {
- // 获取当前页面的网址
- var currentPageURL = window.location.href;
- TK(currentPageURL);
- });
-
- function threeM(currentPageURL) {
- fetch("https://3mw.cn/api/url/shorten/", {
- method: "POST",
- headers: {
- Host: "3mw.cn",
- "Content-Length": "17",
- Origin: "https://uutool.cn",
- Referer: "https://uutool.cn/",
- "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
- },
- body: "url=" + encodeURIComponent(currentPageURL)
- })
- .then(response => {
- if (!response.ok) {
- toast("json获取失败");
- }
- return response.json();
- })
- .then(data => {
- if(data.status === 1) {
- var shorten_url = data.data.shorten_url;
- GM_setClipboard(shorten_url);
- toast("已复制:"+shorten_url, 2);
- } else {
- toast(data.error);
- }
- })
- .catch(error => {
- toast("缩短失败,请再次尝试,如仍然失败请携带网址反馈");
- });
- }
-
- function TK(currentPageURL) {
- // 发送POST请求
- fetch('https://d.glf2ym.cn', {
- method: 'POST',
- headers: {
- 'Host': 'd.glf2ym.cn',
- 'Content-Type': 'application/json',
- 'Origin': 'http://d.glf2ym.cn',
- 'Referer': 'http://d.glf2ym.cn/'
- },
- body: JSON.stringify({ url: currentPageURL })
- })
- .then(response => {
- if (!response.ok) {
- toast("json获取失败");
- }
- return response.json();
- })
- .then(data => {
- if (data.status === 200) {
- var shortenedURL = "https://d.glf2ym.cn" + data.key;
- GM_setClipboard(shortenedURL);
- toast("已复制:" + shortenedURL, 2);
- } else if (data.status === 500) {
- toast(data.key.replace(": Error:", ""), 2);
- threeM(currentPageURL);
- } else {
- toast(data.status+":缩短失败");
- }
- })
- .catch(error => {
- toast("缩短失败,请再次尝试,如仍然失败请携带网址反馈");
- });
- }
-
- })();