Greasy Fork 支持简体中文。

URL_Shortener_Script

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

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

  1. // ==UserScript==
  2. // @name URL_Shortener_Script
  3. // @namespace Рианти
  4. // @description По нажатию клавиш ctrl+Q с любой веб страницы создает короткую ссылку на нее в сервисе goo.gl и копирует в буфер обмена
  5. // @include *
  6. // @version 1
  7. // @grant GM_xmlhttpRequest
  8. // @grant GM_setClipboard
  9. // ==/UserScript==
  10.  
  11. try{
  12.  
  13. var keyToCode = [];
  14. for (var i = 48; i <= 57; i++) keyToCode[String.fromCharCode(i)] = i;
  15. for (var i = 65; i <= 90; i++) keyToCode[String.fromCharCode(i)] = i;
  16.  
  17. window.addEventListener('keydown',function(e){
  18. if(e.keyCode == keyToCode['Q'] && e.ctrlKey){
  19. e.preventDefault();
  20. requestShortening (window.location.href, function(shortURL){
  21. presentShortenedURL(shortURL);
  22. });
  23. }
  24. });
  25.  
  26. var div = document.createElement('div');
  27. 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>';
  28. document.body.appendChild(div);
  29.  
  30. } catch (e) { console.log(e) }
  31.  
  32. function presentShortenedURL(link){
  33. try{
  34. new Audio('http://www.soundjay.com/button/button-15.mp3').play();
  35. document.querySelector('#urlShortenerDivInner').innerHTML = link;
  36. document.querySelector('#urlShortenerDiv').style['visibility'] = 'inherit';
  37. setTimeout(function(){
  38. document.querySelector('#urlShortenerDiv').style['visibility'] = 'hidden';
  39. }, 1000);
  40. GM_setClipboard (link);
  41. console.log(link);
  42. } catch (er) { console.log(er) }
  43. }
  44.  
  45. function requestShortening (pageURL, onloadHandler){
  46. console.log('[URL_Shortener_Script] creating shortcut for: ', pageURL);
  47. try{
  48. GM_xmlhttpRequest({
  49. method: "POST",
  50. synchronous: false,
  51. url: "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyCKraBCeTbm_ZmaQNZ5LZ3Fej_YNUIxKeg",
  52. data: JSON.stringify({ longUrl: pageURL }),
  53. headers: { "Content-Type": "application/json" },
  54. onload: function(response) {
  55. onloadHandler(JSON.parse(response.responseText).id);
  56. },
  57. onerror: function(){ setTimeout( function() { requestPage (url, onloadHandler) }, 500 ) },
  58. ontimeout: function(){ requestPage (url, onloadHandler) },
  59. timeout: 5000
  60. });
  61. } catch (e) {
  62. console.log(e);
  63. }
  64. }