-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentLinkTitler.user.js
141 lines (112 loc) · 3.96 KB
/
CommentLinkTitler.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ==UserScript==
// @name Comment link filter
// @description A hook to transform raw links to properly titled links in comments
// @include http://stackoverflow.com/*
// @include http://meta.stackoverflow.com/*
// @include http://superuser.com/*
// @include http://meta.superuser.com/*
// @include http://serverfault.com/*
// @include http://meta.serverfault.com/*
// @include http://askubuntu.com/*
// @include http://meta.askubuntu.com/*
// @include http://answers.onstartups.com/*
// @include http://meta.answers.onstartups.com/*
// @include http://stackapps.com/*
// @include http://*.stackexchange.com/*
// @exclude http://chat.stackexchange.com/*
// @exclude http://chat.*.stackexchange.com/*
// @exclude http://api.*.stackexchange.com/*
// @exclude http://data.stackexchange.com/*
// @exclude http://*/reputation
// @author @TimStone
// ==/UserScript==
function inject() {
for (var i = 0; i < arguments.length; ++i) {
if (typeof(arguments[i]) == 'function') {
var script = document.createElement('script');
script.type = 'text/javascript';
script.textContent = '(' + arguments[i].toString() + ')(jQuery)';
document.body.appendChild(script);
}
}
}
inject(function ($) {
function HijackedTextarea(t) {
var textarea = t.addClass('link-hijacked'),
form = textarea.closest('form'),
link = new RegExp('(?:^|[^\\(])http://([^\\s/]+)/q(?:uestions)?/([0-9]+)', 'ig'),
lock = 0,
submitComment = form.data('events').submit[0].handler,
validSites = /^(?:(?:(?:meta\.)?(?:stackoverflow|[^.]+\.stackexchange|serverfault|askubuntu|superuser))|stackapps)\.com$/i,
results = [];
form.data('events').submit[0].handler = handler;
function handler() {
if (lock)
return;
lock = -1;
var url, ids = {}, count = 0;;
while (url = link.exec(textarea.val())) {
if (!ids[url[1]])
ids[url[1]] = []
ids[url[1]].push(url[2]);
++count;
}
if (count)
request(ids, callback);
else
submit.call(form.eq(0));
link.lastIndex = 0;
return false;
}
function callback(data, domain) {
lock = lock - 1 === 0 ? -1 : lock - 1;
if (!data.questions || !data.questions.length)
return;
data.domain = domain;
results.push(data);
if (lock < 0) {
submit();
}
}
function submit() {
var i, j, pattern, value = textarea.val();
if (results.length) {
for (i = 0; i < results.length; ++i) {
for (j = 0; j < results[i].questions.length; ++j) {
pattern = '(?:^|[^\\(])http://' + results[i].domain + '/(q(?:uestions)?)/' + results[i].questions[j].question_id + '(?:/[^\\s/]*)?(/[0-9]+)?(#[^\\s]+)?';
value = value.replace(new RegExp(pattern, 'i'), function (s, question, trailing, anchor) {
trailing = trailing || '';
anchor = anchor || '';
return '[' + results[i].questions[j].title + '](http://' + results[i].domain + '/' +
(question === 'questions' && trailing === '' ? 'q' : question) + '/' + results[i].questions[j].question_id +
(question === 'q' ? '' : trailing) + anchor + ')';
});
}
}
textarea.val(value).keyup();
}
submitComment.call(form.eq(0));
results = [];
lock = 0;
}
function request(ids, callback) {
for (var domain in ids) {
if (validSites.test(domain)) {
lock = lock < 0 ? 1 : lock + 1;
$.getJSON('http://api.' + domain + '/1.1/questions/' + ids[domain].join(';') + '?jsonp=?',
(function (d) {
var domain = d;
return function (data) {
callback(data, domain);
};
})(domain));
}
}
}
}
$(document).ready(function () {
$('textarea[name="comment"]:not(.link-hijacked)').live('focus', function () {
new HijackedTextarea($(this));
});
});
});