tinyurl.com 缩短网址按钮

在页面左下角设置一个缩短网址的按钮,这会开个新视窗来查看 tinyurl 的缩址结果

目前为 2022-05-15 提交的版本。查看 最新版本

  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. It will create a new window for the shortened URL of tinyurl
  6. // @description:zh-TW 在頁面左下角設置一個縮短網址的按鈕,這會開個新視窗來檢視 tinyurl 的縮址結果
  7. // @description:zh-CN 在页面左下角设置一个缩短网址的按钮,这会开个新视窗来查看 tinyurl 的缩址结果
  8. // @namespace https://greasyfork.org/zh-TW/users/393133-evan-tseng
  9. // @version 0.30
  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("triggerBy") == "shortenButton") {
  22. var tu = document.body.innerText;
  23. document.body.innerHTML = "";
  24. if(tu.indexOf("https://tinyurl.com/") == 0) {
  25. document.write(`<style>
  26. html, body { background:#ccc!important }
  27. body { text-align:center; padding:3px 0; margin:0; vertical-align:middle }
  28. #sUrl, #ccBtn { font-size:14px; height:24px; line-height:24px; vertical-align:middle; padding:0 .5em; }
  29. #sUrl { width:15em; text-align:center; color:222; border:1px inset #eee; border-radius:1mm }
  30. #ccBtn { width:4em; color:#000; background:#ddd; border:1px solid #888; border-radius:2mm; 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. document.write('<div><input id="sUrl" value="' + tu + '" readonly/> <button id="ccBtn" onclick="copyUrl()">Copy</button></div>');
  43. document.write(`<script>
  44. function copyUrl() {
  45. let txtBox = document.querySelector("#sUrl");
  46. txtBox.focus();
  47. txtBox.select();
  48. document.execCommand("copy");
  49. txtBox.setSelectionRange(0,0)
  50. window.close();
  51. }
  52. document.querySelector("#ccBtn").focus();
  53. </script>`);
  54. }
  55. else {
  56. document.write('<style> body { font:400 12pt sans-serif; color:#eee; background:#333; padding:0; margin:0; } </style>');
  57. document.write('<p>Please logout your TinyURL account.<br/>Try using the shorten button script as anonymous.</p>');
  58. }
  59. }
  60. }
  61. else if(window.self === window.top) {
  62. const TUcss = `
  63. .__TUwrap__ { position:fixed; left: 0; bottom:33mm; z-index:22222222 }
  64. .__TUbg__ { position:fixed; top:0; left:0; display:none; background:rgba(0,0,0,.5); width:100vw; height:100vh; z-index:-1; backdrop-filter:blur(1mm); -webkit-backdrop-filter:blur(1mm) }
  65. .__TUbtn__ { position:absolute; left:-2mm; transform:rotate(90deg); font:400 12pt sans-serif!important; width:5em!important; color:#333!important; background:#ddd!important; margin:0 -1.8em!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; cursor:pointer; transition:.3s; }
  66. .__TUbtn__:hover { left:0; box-shadow: 2px 0 2mm 1px rgba(0,0,0,.5); opacity:1; transition:.1s;}
  67. .__TUbtn__:active { color:#eee!important; background:#666!important; box-shadow: inset 1px 0 1mm 1px rgba(0,0,0,.5); }
  68. .__TUbox__ { position:absolute; left:10mm; top:-4mm; display:none; padding:3mm; border-radius:3mm; background:#ccc; box-shadow:0 1mm 5mm rgba(0,0,0,.3); backdrop-filter:blur(3mm); -webkit-backdrop-filter:blur(3mm)}
  69. .__TUbox__:before { position:absolute; top:20px; left:-7px; display:block; content:""; border-top:7px solid transparent;border-bottom:7px solid transparent; z-index:2 }
  70. .__TUpage__ { display:block!important; width:300px; height:32px; background:transparent; background-image:none; border:none; }
  71.  
  72. .__TUbox__ { background:#ccc; box-shadow:0 1mm 5mm rgba(0,0,0,.3) }
  73. .__TUbox__:before { border-right:7px solid #ccc; }
  74. `;
  75. var cssStyle = document.createElement('style');
  76. if(cssStyle.styleSheet) cssStyle.styleSheet.cssText = TUcss;
  77. else cssStyle.appendChild(document.createTextNode(TUcss));
  78. document.querySelector('head').appendChild(cssStyle);
  79.  
  80. var TU = function() {
  81. var TUbg = null,
  82. TUwrap = null,
  83. TUbtn = null,
  84. TUbox = null,
  85. TUpage = null,
  86. queryURL = null;
  87.  
  88. const init = function(){
  89. if(TUwrap == null) {
  90. TUwrap = document.createElement('div');
  91. TUwrap.setAttribute('class', '__TUwrap__');
  92. TUbg = document.createElement('div');
  93. TUbg.setAttribute('class', '__TUbg__');
  94. TUbtn = document.createElement('button');
  95. TUbtn.setAttribute('class', '__TUbtn__');
  96. TUbtn.innerText = "TinyURL"
  97. TUbox = document.createElement('div');
  98. TUbox.setAttribute('class', '__TUbox__');
  99.  
  100. TUwrap.appendChild(TUbg);
  101. TUwrap.appendChild(TUbtn);
  102. TUwrap.appendChild(TUbox);
  103. document.body.appendChild(TUwrap);
  104. TUbg.addEventListener('click', function(){ close(); });
  105. TUbtn.addEventListener('click', function(){ query(location.href) });
  106. }
  107. }
  108.  
  109. const query = function(theUrl){
  110. close();
  111. queryURL = 'https://tinyurl.com/api-create.php?triggerBy=shortenButton&url=' + encodeURIComponent(theUrl);
  112. TUpage = document.createElement('iframe');
  113. TUpage.setAttribute('class', '__TUpage__');
  114. TUpage.src = queryURL;
  115. TUbox.appendChild(TUpage);
  116. TUbox.setAttribute('style', 'display:block;');
  117. TUbg.setAttribute('style', 'display:block')
  118.  
  119. }
  120.  
  121. const close = function(){
  122. if(TUpage) {
  123. TUbox.setAttribute('style', 'display:none;');
  124. TUpage.remove();
  125. TUpage = null;
  126. }
  127. TUbg.setAttribute('style', 'display:none')
  128. }
  129.  
  130. init();
  131. }
  132. TU();
  133. }
  134.  
  135. })();