-
Notifications
You must be signed in to change notification settings - Fork 0
/
minify_twitter_urls.user.js
124 lines (115 loc) · 4.67 KB
/
minify_twitter_urls.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Minify URLs For Twitter
// version 1.0.1
// 2009-05-26
// Copyright (c) 2009, Nate Agrin
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a beta script that tries to solve a problem I'd been having while
// using Twitter's web interface. I would notice that Twitter was smart enough to
// shorten urls when I was under the 140 char limit (using tinyurl.com), but if I
// was over because of a url it wouldn't let me submit. This forced me to either
// tweet much smaller messages than I really was allowed or go to tinyurl or
// is.gd to shorten the url then paste it into the tweet by hand (which I'm too
// lazy to do).
//
// Now as you type, if you press space any recognized urls are shortened
// automatically. Urls are also shortened before you submit to Twitter and if you
// paste in some text which puts the total tweet length over 140 char.
//
// Disclaimer:
// This is a beta script; I wrote it in a couple of hours and so you
// should use it at your own risk. I did use 'unsafeWindow' and 'windowJSObject'
// twice which may pose security risks. I ASSUME NO LIABILITY IF BAD THINGS
// HAPPEN TO YOU OR YOUR COMPUTER WHILE USING THIS SCRIPT. But I doubt that will
// happen.
//
// Todo:
// 1) It would be nice if the script recognized "foo.com" as a url and
// shortened it so you have to type less.
//
// 2) It would be nice if when you put the cursor on a shortened url, it
// displayed the original url so that you could make any corrections or edits to
// the original url. The idea being that if you accidentally hit space or wanted
// to double check your urls you could before submitting.
//
// 3) It would be nice to abstract the shortening code a bit more and allow for
// others to write plugins for tinyurl and other shortening services.
//
// --------------------------------------------------------------------
// ==UserScript==
// @name Minify URLs for Twitter
// @namespace http://n8agrin.com/
// @description Minify recognizes any URLs in what you are typing and automatically shortens them using the "is.gd" service.
// @include http://twitter.com/*
// @include https://twitter.com/*
// ==/UserScript==
window.addEventListener('load', function() {
if (!GM_xmlhttpRequest) {
throw 'Your version of GreaseMonkey does not include the \
GM_xmlhttpRequest method which is required for this grease script. \
Please to upgradez.';
return;
}
var status = document.getElementById('status');
var http_re = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
var find_and_replace = function(status, callback) {
var values = status.value.split(' ');
var last_value = values.pop();
if (http_re.test(last_value)) {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://is.gd/create.php?longurl=' + encodeURIComponent(last_value),
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(response) {
var response_html = response.responseText;
var short_url = response_html.match(/id="short_url"\svalue="([^\s]+)"/);
if (short_url.constructor === Array && short_url.length === 2) {
if (values.length === 0) {
status.value = short_url[1] + ' ';
}
else {
status.value = (values.join(' ') + ' ' + short_url[1] + ' ');
}
// ACCESSING THE UNSAFEWINDOW!!!
unsafeWindow.$(status).change();
}
if (typeof callback === 'function'){
callback();
}
}
});
}
else {
if (typeof callback === 'function'){
callback();
}
}
};
// replace urls when the user hits 'space'
status.addEventListener('keydown', function(event) {
if (event.keyCode === 32) {
find_and_replace(this);
}
}, false);
// replace urls when the length is greater than 140 and the user probably
// pasted in a long url
status.addEventListener('keyup', function(event) {
if (this.value.length > 140) {
find_and_replace(this);
}
}, false);
// ACCESSING THE RAW SUBMIT BUTTON!!!!
var submit_button = document.getElementById('update-submit').wrappedJSObject;
var old_submit_event = submit_button.onclick;
submit_button.onclick = null;
// replace the last url added if they are submitting
document.getElementById('update-submit').addEventListener('click', function() {
find_and_replace(status, old_submit_event);
}, false);
}, false);