URL_Shortener_Script

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

Від 05.01.2016. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
    }
}