NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @name TinyURL!
// @version 1.3
// @description You'll be able to make a TinyURL at the click of a button. By clicking on the menu command, a TinyURL will be created for the page you are currently at.
// @license MIT
// @author Volkan K.
// @copyright 2014-2018+, Volkan K.
// @namespace volkank@openuserjs
// @include *
// @datecreated 2014-11-19
// @lastupdated 2018-06-13
// @run-at document-start
// @grant GM_registerMenuCommand
// @grant GM_openInTab
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js
// @connect tinyurl.com
// ==/UserScript==
var tiny_method = 2;
// 1=open tinyurl.com creation page in new tab
// 2=show the result as alert without opening tab or window.
var debug_internal = 1; // 1=enable debug , 0 =disable debug
if (typeof GM_openInTab == "undefined") {
GM_openInTab = window.open;
}
function debugLog(message, do_alert) {
if (debug_internal==1) {
console.log("USER-SCRIPT TINYURL | " + message);
}
if (do_alert && typeof alert === "function") {
alert("USER-SCRIPT TINYURL | " + message);
}
}
function show_tinyurl (short_url, long_url) {
// do stuff
//console.log(long_url); // for debug only.
if (!(long_url)){
var long_url = window.location.href;
}
var p_message = 'Long URL:'+"\n"+long_url+"\n\n";
//p_message += 'Short URL:';
p_message += 'It has a length of '+long_url.length+' characters and resulted in the following'+"\n"+'TinyURL which has a length of '+short_url.length+' characters:'
p_message += "\n"+short_url+"\n ";
if (typeof GM_setClipboard != "undefined") {
GM_setClipboard(short_url);
p_message += "\n"+'=== TinyURL Copied to your clipboard ==='+"\n ";
}
prompt(p_message,short_url);
}
function parse_n_show_tinyurl(html, long_url){
if (!(long_url)){
var long_url = window.location.href;
}
var response_parsed=$.parseHTML(html);
var contentcontainer= $('div#contentcontainer', response_parsed).html();
console.log(contentcontainer);
var myregexp = /<div class="indent"><b>(https?:\/\/tinyurl\.com\/[-\w]+)<\/b>/i;
var match = myregexp.exec(contentcontainer);
if (match != null) {
short_url = match[1];
} else {
short_url = "";
debugLog("ERROR: Failed to get short url for "+long_url,true);
return;
}
show_tinyurl (short_url, long_url);
}
GM_registerMenuCommand("TinyURL!", function(){
var current_location = encodeURIComponent(window.location.href);
var custom_alias = prompt("Custom alias (optional):\nMay contain letters, numbers, and dashes.\nhttp://tinyurl.com/");
if (custom_alias!="") {
tiny_method=1;
custom_alias=encodeURIComponent(custom_alias);
}
if (tiny_method==1){
var my_url = 'http://tinyurl.com/create.php?url='+current_location;
if (custom_alias!="") {
my_url += '&alias='+custom_alias;
}
debugLog(my_url); //for debugging
//GM_openInTab(my_url);
GM_xmlhttpRequest({
method: "GET",
url: my_url,
onerror: function(oEvent){ debugLog("Error " + oEvent.status + " occurred while receiving the document.",true); },
onload: function(response){
if (response.readyState !== 4 || response.status !== 200) {
debugLog("ERROR: Response status is not OK (200) but "+response.status, true); return;
}
// show result
parse_n_show_tinyurl(response.responseText);
}
});
} else {
GM_xmlhttpRequest({
method: "GET",
url: 'http://tinyurl.com/api-create.php?url='+current_location,
onerror: function(oEvent){ debugLog("Error " + oEvent.status + " occurred while receiving the document.",true); },
onload: function(response){
if (response.readyState !== 4 || response.status !== 200) {
debugLog("ERROR: Response status is not OK (200) but "+response.status, true); return;
}
// show result
show_tinyurl(response.responseText);
}
});
}
});