tinyurl.com 縮短網址按鈕

在頁面左下角設置一個縮短網址的按鈕,點按即可從 tinyurl.com 取得目前頁面的縮址

目前為 2022-06-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name				tinyurl.com - URL shorten button
// @name:zh-TW			tinyurl.com 縮短網址按鈕
// @name:zh-CN			tinyurl.com 缩短网址按钮
// @description			Add a URL shorten button to the bottom left corner. Click it to get a shortened URL (by tinyurl.com) of current page.
// @description:zh-TW	在頁面左下角設置一個縮短網址的按鈕,點按即可從 tinyurl.com 取得目前頁面的縮址
// @description:zh-CN	在页面左下角设置一个缩短网址的按钮,点按即可从 tinyurl.com 取得当前页面的缩址
// @namespace			https://greasyfork.org/zh-TW/users/393133-evan-tseng
// @version				0.33
// @author				Evan Tseng
// @include				*://*
// @grant				none
// @run-at				document-body
// @license				MIT
// ==/UserScript==

(function() {
	'use strict';
	if(location.hostname == "tinyurl.com") {
		var url = new URL(location.href);
		if(url.searchParams.get("trigger") == "shortenButton") {
			var tu = document.body.innerText;
			document.body.innerHTML = "";
			if(tu.indexOf("https://tinyurl.com/") == 0) {
				document.write(`<style>
html, body { background:#bbb; overflow:hidden }
body { width:300px; height:32px; text-align:center; padding:3px 0; margin:0; vertical-align:middle }
#sUrl, #ccBtn { font-size:14px; height:24px; line-height:24px; vertical-align:middle; padding:0 .5em; }
#sUrl { width:15em; text-align:center; color:222; border:1px inset #eee; border-radius:1mm }
#ccBtn { width:4em; color:#000; background:#ddd; border:1px solid #888; border-radius:2mm; cursor:pointer }
input, button { outline:none }
input:focus, button:focus { box-shadow:0 0 1mm 1px #08f }
@media (prefers-color-scheme: light) {
	#ccBtn:hover { filter:brightness(1.2) }
	#ccBtn:active { filter:brightness(.9) }
}
@media (prefers-color-scheme: dark) {
	#ccBtn:hover { filter:brightness(1.5) }
	#ccBtn:active { filter:brightness(.75) }
}
</style>`);
				document.write('<div><input id="sUrl" value="' + tu + '" readonly/> <button id="ccBtn" onclick="copyUrl()">Copy</button></div>');
				document.write(`<script>
function copyUrl() {
	let txtBox = document.querySelector("#sUrl");
	txtBox.focus();
	txtBox.select();
	document.execCommand("copy");
	txtBox.setSelectionRange(0,0)
	window.close();
}
document.querySelector("#ccBtn").focus();
</script>`);
			}
			else if(tu.indexOf('Error') == 0) {
				document.write(`
<style> body { font:400 10pt sans-serif; color:#222; background:#ccc; padding:0; margin:0; } </style>
<p>Unable to shorten this URL.</p>`);
			}
			else {
				document.write(`
<style> body { font:400 10pt sans-serif; color:#222; background:#ccc; padding:0; margin:0; } </style>
<p>Please logout your TinyURL account. Try using the shorten button script with anonymous.</p>`);
			}
		}
	}
	else if(window.self === window.top) {
		(function(){
			const TUcss = `
.__TUwrap__ { position:fixed; left: 0; bottom:33mm; z-index:22222222 }
.__TUbg__ { position:fixed; top:0; left:0; background:rgba(0,0,0,.5); width:100vw; height:100vh; z-index:-1; backdrop-filter:blur(2mm); -webkit-backdrop-filter:blur(2mm); visibility:hidden; opacity:0; transition:.5s, width 0s, height 0s }
.__TUbtn__ { position:absolute; left:-6pt; transform:rotate(90deg); font:400 12pt sans-serif!important; color:#333!important; background:#ddd!important; padding:0 .5em!important; margin:0 -20pt!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; }
.__TUbtn__:hover { left:-1px; box-shadow: 2px 0 2mm 1px rgba(0,0,0,.5); opacity:1; transition:.1s;}
.__TUbtn__:active { color:#eee!important; background:#666!important; box-shadow: inset 1px 0 1mm 1px rgba(0,0,0,.5); transition:.07s}
.__TUbox__ { position:absolute; left:10mm; top:-4mm; display:none; box-sizing:content-box; width:300px; height:32px; 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)}
.__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 }
.__TUpage__ { display:block!important; width:300px; height:32px; background:transparent; background-image:none; border:none; }`;
			var cssStyle = document.createElement('style');
			if(cssStyle.styleSheet)	cssStyle.styleSheet.cssText = TUcss;
			else	cssStyle.appendChild(document.createTextNode(TUcss));
			document.querySelector('head').appendChild(cssStyle);

			var TUwrap = null,
				TUbg = null,
				TUbox = null,
				TUpage = null;

			function query(theUrl){
				close();
				let queryURL = 'https://tinyurl.com/api-create.php?trigger=shortenButton&url=' + encodeURIComponent(theUrl);
				TUpage = document.createElement('iframe');
				TUpage.setAttribute('class', '__TUpage__');
				TUpage.src = queryURL;
				TUbox.appendChild(TUpage);
				TUbox.setAttribute('style', 'display:block;');
				TUbg.setAttribute('style', 'visibility:visible; opacity:1')
			}

			function close(){
				if(TUpage) {
					TUbox.setAttribute('style', 'display:none');
					TUpage.remove();
					TUpage = null;
				}
				TUbg.setAttribute('style', 'visibility:hidden; opacity:0')
			}

			if(TUwrap == null) {
				TUwrap = document.createElement('div');
				TUwrap.setAttribute('class', '__TUwrap__');
				TUbg = document.createElement('div');
				TUbg.setAttribute('class', '__TUbg__');
				let TUbtn = document.createElement('button');
				TUbtn.setAttribute('class', '__TUbtn__');
				TUbtn.innerText = "TinyURL"
				TUbox = document.createElement('div');
				TUbox.setAttribute('class', '__TUbox__');

				TUwrap.appendChild(TUbg);
				TUwrap.appendChild(TUbtn);
				TUwrap.appendChild(TUbox);
				document.body.appendChild(TUwrap);
				TUbg.addEventListener('click', close );
				TUbtn.addEventListener('click', function(){ query(location.href) });
			}
		})();
	}

})();