tinyurl.com 缩短网址按钮

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

目前为 2022-07-02 提交的版本,查看 最新版本

  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.35
  10. // @author Evan Tseng
  11. // @include *://*
  12. // @grant none
  13. // @run-at document-body
  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 { color:#000; background:#bbb; overflow:hidden }
  26. body { width:236pt; height:24pt; font:400 12pt/20pt sans-serif; text-align:center; padding:0 2pt; margin:0; vertical-align:middle }
  27. .errMsg { color:#600; font-weight:600; line-height:24pt; }
  28. #sUrl, #ccBtn { font:400 12pt/20pt sans-serif; height:20pt; line-height:1; text-align:center; vertical-align:middle; padding:0 .5em; margin:2pt 0 }
  29. #sUrl { width:160pt; text-align:center; color:222; border:1px inset #eee; border-radius:5pt }
  30. #ccBtn { width:4em; color:#000; background:#ddd; border:1px solid #888; border-radius:5pt; cursor:pointer }
  31. input, button { outline:none }
  32. input:focus, button:focus { box-shadow:0 0 1mm 1px #08f }
  33. @media (prefers-color-scheme: light) {
  34. #ccBtn:hover { filter:brightness(1.2) }
  35. #ccBtn:active { filter:brightness(.9) }
  36. }
  37. @media (prefers-color-scheme: dark) {
  38. #ccBtn:hover { filter:brightness(1.5) }
  39. #ccBtn:active { filter:brightness(.75) }
  40. }
  41. </style>`);
  42. if(tu.match(/^https?:\/\/tinyurl\.com\/\w+$/)) {
  43. document.write('<div><input id="sUrl" value="' + tu + '" readonly/> <button id="ccBtn" onclick="copyUrl()">Copy</button></div>');
  44. document.write(`<script>
  45. function copyUrl() {
  46. let txtBox = document.querySelector("#sUrl");
  47. txtBox.focus();
  48. txtBox.select();
  49. document.execCommand("copy");
  50. txtBox.setSelectionRange(0,0)
  51. window.close();
  52. }
  53. document.querySelector("#ccBtn").focus();
  54. </script>`);
  55. }
  56. else document.write('<div class="errMsg">Error! Unable to shorten this URL.</div>');
  57. }
  58. }
  59. else if(window.self === window.top) {
  60. (function(){
  61. const TUcss = `
  62. .__TUwrap__ { position:fixed; left:0; bottom:33mm; width:0; margin:0; z-index:2147483647 }
  63. .__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 }
  64. .__TUbtn__ { position:absolute; left:.2em; transform:rotate(90deg); width:64pt; font:400 12pt/1.6 arial!important; color:#333!important; background:#ddd!important; padding:0!important; margin:0 -32pt!important; line-height:1.6!important; border:1px solid #888; border-radius:5px 5px 0 0; box-shadow:0 0 0 1px rgba(0,0,0,.4); opacity:.4; white-space:nowrap; cursor:pointer; transition:.3s,left .5s .5s; }
  65. .__TUbtn__:hover { left:.8em; box-shadow: 2px 0 2mm 1px rgba(0,0,0,.5); opacity:1; transition:.1s;}
  66. .__TUbtn__:active { color:#eee!important; background:#666!important; box-shadow: inset 1px 0 1mm 1px rgba(0,0,0,.5); transition:0s}
  67. .__TUbox__ { position:absolute; left:10mm; top:-4mm; display:none; box-sizing:content-box; width:236pt; height:24pt; padding:3mm; border-radius:3mm; background:#bbb; box-shadow:0 1mm 5mm rgba(0,0,0,.3); backdrop-filter:blur(3mm); -webkit-backdrop-filter:blur(3mm)}
  68. .__TUbox__:before { position:absolute; top:20px; left:-7px; display:block; content:""; border-top:7px solid transparent;border-bottom:7px solid transparent; border-right:7px solid #bbb; z-index:2 }
  69. .__TUpage__ { display:block!important; width:236pt; height:24pt; background:transparent; background-image:none; border:none; }`;
  70.  
  71. let cssStyle = document.createElement('style');
  72. if(cssStyle.styleSheet) cssStyle.styleSheet.cssText = TUcss;
  73. else cssStyle.appendChild(document.createTextNode(TUcss));
  74. document.querySelector('head').appendChild(cssStyle);
  75.  
  76. var TUwrap = null,
  77. TUbg = null,
  78. TUbox = null,
  79. TUpage = null;
  80.  
  81. function query(theUrl){
  82. close();
  83. let queryURL = 'https://tinyurl.com/api-create.php?trigger=shortenButton&url=' + encodeURIComponent(theUrl);
  84. TUpage = document.createElement('iframe');
  85. TUpage.setAttribute('class', '__TUpage__');
  86. TUpage.src = queryURL;
  87. TUbox.appendChild(TUpage);
  88. TUbox.setAttribute('style', 'display:block;');
  89. TUbg.setAttribute('style', 'visibility:visible; opacity:1')
  90. }
  91.  
  92. function close(){
  93. if(TUpage) {
  94. TUbox.setAttribute('style', 'display:none');
  95. TUpage.remove();
  96. TUpage = null;
  97. }
  98. TUbg.setAttribute('style', 'visibility:hidden; opacity:0')
  99. }
  100.  
  101. function initialize(){
  102. TUwrap = document.createElement('div');
  103. TUwrap.setAttribute('class', '__TUwrap__');
  104. TUbg = document.createElement('div');
  105. TUbg.setAttribute('class', '__TUbg__');
  106. let TUbtn = document.createElement('button');
  107. TUbtn.setAttribute('class', '__TUbtn__');
  108. TUbtn.innerText = "TinyURL"
  109. TUbox = document.createElement('div');
  110. TUbox.setAttribute('class', '__TUbox__');
  111.  
  112. TUwrap.appendChild(TUbg);
  113. TUwrap.appendChild(TUbtn);
  114. TUwrap.appendChild(TUbox);
  115. document.body.appendChild(TUwrap);
  116. TUbg.addEventListener('click', close );
  117. TUbtn.addEventListener('click', function(){ query(window.location.href) });
  118. }
  119.  
  120. if(TUwrap == null) initialize();
  121. })();
  122. }
  123.  
  124. })();