tinyurl.com 缩短网址按钮

在页面左下角设置一个缩短网址的按钮,点按即可从 tinyurl.com 取得当前页面的缩址

目前为 2022-10-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name tinyurl.com - URL shorten button
  3. // @name:zh-TW tinyurl.com 縮短網址按鈕
  4. // @name:zh-CN tinyurl.com 缩短网址按钮
  5. // @description Add a URL shorten button to the bottom left corner. Click it to get a shortened URL (by tinyurl.com) of current page.
  6. // @description:zh-TW 在頁面左下角設置一個縮短網址的按鈕,點按即可從 tinyurl.com 取得目前頁面的縮址
  7. // @description:zh-CN 在页面左下角设置一个缩短网址的按钮,点按即可从 tinyurl.com 取得当前页面的缩址
  8. // @namespace https://greasyfork.org/zh-TW/users/393133-evan-tseng
  9. // @version 0.39
  10. // @author Evan Tseng
  11. // @match *://*/*
  12. // @grant none
  13. // @run-at document-end
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19. if(location.hostname == "tinyurl.com") {
  20. var url = new URL(location.href);
  21. if(url.searchParams.get("trigger") == "shortenButton") {
  22. var tu = document.body.innerText;
  23. document.body.innerHTML = "";
  24. document.write(`<style>
  25. html, body { overflow:hidden }
  26. body { max-width:240pt; height:24pt; font:400 12pt/1.4 sans-serif; text-align:center; padding:0 2pt; margin:0; vertical-align:middle }
  27. .wrap { display:flex; }
  28. .errMsg { color:#600; font-weight:600; line-height:24pt; }
  29. #sUrl, #ccBtn { font:400 12pt/20pt sans-serif; height:auto; text-align:center; vertical-align:middle; padding:0 .5em; margin:2pt 0; border:none; border-radius:5pt; }
  30. #sUrl { flex:1 1 auto; width:160pt; text-align:center; }
  31. #ccBtn { flex:0 0 4em; width:auto; margin-left:1mm; color:#000; background:#ddd; cursor:pointer }
  32. input, button { outline:none }
  33. input:focus, button:focus { box-shadow:0 0 1mm 1px #08f!important }
  34. @media (prefers-color-scheme: light) {
  35. html, body { color:#333; background:#bbb }
  36. #sUrl, #ccBtn { color:#333; background:#eee; box-shadow:0 0 0 1px #888 }
  37. #ccBtn:hover { background: #fff }
  38. #ccBtn:active { color:#800; background: #aaa }
  39. }
  40. @media (prefers-color-scheme: dark) {
  41. html, body { color:#ccc; background:#444 }
  42. #sUrl, #ccBtn { color:#ccc; background:#222; box-shadow:0 0 0 1px #666 }
  43. #ccBtn:hover { background: #333 }
  44. #ccBtn:active { color:#faa; background: #000 }
  45. }
  46.  
  47. </style>`);
  48. if(tu.match(/^https?:\/\/tinyurl\.com\/\w+$/)) {
  49. document.write('<div class="wrap"><input id="sUrl" value="' + tu + '" readonly/> <button id="ccBtn" tabindex="1" onclick="copyUrl()">Copy</button></div>');
  50. document.write(`<script>
  51. function copyUrl() {
  52. let txtBox = document.querySelector("#sUrl");
  53. txtBox.focus();
  54. txtBox.select();
  55. document.execCommand("copy");
  56. txtBox.setSelectionRange(0,0)
  57. window.close();
  58. document.querySelector("#ccBtn").focus();
  59. }
  60. </script>`);
  61. }
  62. else document.write('<div class="errMsg">Error! Unable to shorten this URL.</div>');
  63. }
  64. }
  65. else if(window.self === window.top) {
  66. (function(){
  67. const TUcss = `
  68. @media screen {
  69. .__TUwrap__ { position:fixed!important; left:0!important; bottom:33mm!important; width:0!important; min-width:0!important; margin:0!important; z-index:2147483647!important }
  70. .__TUbg__ { position:fixed; top:0; left:0; background:rgba(0,0,0,.5); width:100vw; height:100vh; z-index:-1; backdrop-filter:blur(3mm); -webkit-backdrop-filter:blur(3mm); visibility:hidden; opacity:0; transition:.5s, width 0s, height 0s }
  71. .__TUbtn__ { position:absolute; left:.2em; transform:rotate(90deg); width:64pt; font:400 12pt/1.6 arial!important; text-align:center!important; padding:0!important; margin:0 -32pt!important; border-radius:5px 5px 0 0; opacity:.4; white-space:nowrap; cursor:pointer; transition:.3s,left .5s .5s; }
  72. .__TUbtn__.active, .__TUbtn__:hover { left:.8em; opacity:1; transition:.1s;}
  73. .__TUbtn__:active { box-shadow: inset 1px 0 1mm 1px rgba(0,0,0,.5); transition:0s}
  74. .__TUbox__ { position:absolute; left:10mm; top:-4mm; display:none; box-sizing:content-box; width:244pt; height:24pt; padding:3mm; border-radius:3mm; backdrop-filter:blur(3mm); -webkit-backdrop-filter:blur(3mm)}
  75. .__TUbox__::before { position:absolute; top:20px; left:-7px; display:block; content:""; border-top:7px solid transparent;border-bottom:7px solid transparent; z-index:2 }
  76. .__TUbox__::after { position:absolute; top:20px; left:-8px; display:block; content:""; border-top:7px solid transparent;border-bottom:7px solid transparent; border-right:7px solid #777; z-index:-1 }
  77. .__TUpage__ { display:block!important; width:244pt; height:24pt; background:transparent; background-image:none; border:none; }
  78. }
  79. @media (prefers-color-scheme: light) {
  80. .__TUbtn__ { color:#333!important; background:#bbb!important; border:1px solid #999; box-shadow:0 0 0 1px rgba(0,0,0,.3) }
  81. .__TUbtn__:hover { background:#ddd!important; box-shadow: 2px 0 2mm 1px rgba(0,0,0,.5) }
  82. .__TUbtn__:active { color:#eee!important; background:#777!important }
  83. .__TUbox__ { background:#bbb; box-shadow:0 0 0 1px #777, 0 1mm 5mm rgba(0,0,0,.3) }
  84. .__TUbox__::before { border-right:7px solid #bbb }
  85. }
  86. @media (prefers-color-scheme: dark) {
  87. .__TUbtn__ { color:#ccc!important; background:#444!important; border:1px solid #555; box-shadow:0 0 0 1px rgba(255,255,255,.3) }
  88. .__TUbtn__:hover { background:#333!important; box-shadow: 2px 0 2mm 1px rgba(255,255,255,.5) }
  89. .__TUbtn__:active { color:#ddd!important; background:#222!important; }
  90. .__TUbox__ { background:#444; box-shadow:0 0 0 1px #777, 0 1mm 5mm rgba(255,255,255,.3); }
  91. .__TUbox__::before { border-right:7px solid #444; }
  92. }
  93. @media print {
  94. .__TUwrap__ { display: none }
  95. }`;
  96.  
  97. let cssStyle = document.createElement('style');
  98. if(cssStyle.styleSheet) cssStyle.styleSheet.cssText = TUcss;
  99. else cssStyle.appendChild(document.createTextNode(TUcss));
  100. document.querySelector('head').appendChild(cssStyle);
  101.  
  102. var TUwrap = null,
  103. TUbtn = null,
  104. TUbg = null,
  105. TUbox = null,
  106. TUpage = null;
  107.  
  108. function query(theUrl){
  109. close();
  110. let queryURL = 'https://tinyurl.com/api-create.php?trigger=shortenButton&url=' + encodeURIComponent(theUrl);
  111. TUpage = document.createElement('iframe');
  112. TUpage.classList.add('__TUpage__');
  113. TUpage.src = queryURL;
  114. TUbox.appendChild(TUpage);
  115. TUbox.setAttribute('style', 'display:block;');
  116. TUbg.setAttribute('style', 'visibility:visible; opacity:1')
  117. TUbtn.classList.add('active');
  118. }
  119.  
  120. function close(){
  121. if(TUpage) {
  122. TUbtn.classList.remove('active');
  123. TUbox.setAttribute('style', 'display:none');
  124. TUpage.remove();
  125. TUpage = null;
  126. }
  127. TUbg.setAttribute('style', 'visibility:hidden; opacity:0')
  128. }
  129.  
  130. function initialize(){
  131. TUwrap = document.createElement('div');
  132. TUwrap.classList.add('__TUwrap__');
  133. TUbg = document.createElement('div');
  134. TUbg.classList.add('__TUbg__');
  135. TUbtn = document.createElement('button');
  136. TUbtn.classList.add('__TUbtn__');
  137. TUbtn.innerText = "TinyURL"
  138. TUbox = document.createElement('div');
  139. TUbox.classList.add('__TUbox__');
  140.  
  141. document.documentElement.appendChild(TUwrap);
  142. TUwrap.appendChild(TUbg);
  143. TUwrap.appendChild(TUbtn);
  144. TUwrap.appendChild(TUbox);
  145. TUbg.addEventListener('click', close );
  146. TUbtn.addEventListener('click', function(){ query(window.location.href); });
  147. }
  148.  
  149. if(TUwrap == null) initialize();
  150. })();
  151. }
  152.  
  153. })();