URL_Shortener_Script

По нажатию клавиш ctrl+Q с любой веб страницы создает короткую ссылку на нее в сервисе goo.gl и копирует в буфер обмена

目前為 2016-01-05 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        URL_Shortener_Script
// @namespace   Рианти
// @description По нажатию клавиш ctrl+Q с любой веб страницы создает короткую ссылку на нее в сервисе goo.gl и копирует в буфер обмена
// @include     *
// @version     1
// @grant       GM_xmlhttpRequest
// @grant       GM_setClipboard
// ==/UserScript==

try{

var keyToCode = [];
for (var i = 48; i <= 57; i++) keyToCode[String.fromCharCode(i)] = i;
for (var i = 65; i <= 90; i++) keyToCode[String.fromCharCode(i)] = i;

window.addEventListener('keydown',function(e){
   if(e.keyCode == keyToCode['Q'] && e.ctrlKey){
       e.preventDefault();
       requestShortening (window.location.href, function(shortURL){
           presentShortenedURL(shortURL);
       });
   }
});

var div = document.createElement('div');
div.innerHTML = '<div id="urlShortenerDiv" style="position: fixed; width: 100%; height: 100%; left: 0px; top: 0px; background-color: white; opacity: 0.8; align-content: center; z-index: 999; visibility: hidden;"><div valign="center" style="margin: 0px -50% 0px 0px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 22px;" align="center">Короткая ссылка скопирована в буфер<br><br><i id="urlShortenerDivInner" style="visibility: inherit;"></i></div></div>';
document.body.appendChild(div);

} catch (e) { console.log(e) }

function presentShortenedURL(link){
  try{
  new Audio('http://www.soundjay.com/button/button-15.mp3').play();
  document.querySelector('#urlShortenerDivInner').innerHTML = link;
  document.querySelector('#urlShortenerDiv').style['visibility'] = 'inherit';
  setTimeout(function(){
    document.querySelector('#urlShortenerDiv').style['visibility'] = 'hidden';
  }, 1000);
  GM_setClipboard (link);
  console.log(link);
  } catch (er) { console.log(er) }
}

function requestShortening (pageURL, onloadHandler){
    console.log('[URL_Shortener_Script] creating shortcut for: ', pageURL);
    try{
      GM_xmlhttpRequest({
        method: "POST",
        synchronous: false,
        url: "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyCKraBCeTbm_ZmaQNZ5LZ3Fej_YNUIxKeg",
        data: JSON.stringify({ longUrl: pageURL }),
        headers: { "Content-Type": "application/json" },
        onload: function(response) {
          onloadHandler(JSON.parse(response.responseText).id);
        },
        onerror: function(){ setTimeout( function() { requestPage (url, onloadHandler) }, 500 ) },
        ontimeout: function(){ requestPage (url, onloadHandler) },
        timeout: 5000
      });
    } catch (e) {
        console.log(e);
    }
}