-
Notifications
You must be signed in to change notification settings - Fork 1
/
RevisionCommentLinks.user.js
38 lines (34 loc) · 1.67 KB
/
RevisionCommentLinks.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
// ==UserScript==
// @name Revision Comment Links
// @namespace https://stackexchange.com/users/305991/jason-c
// @version 1.01
// @description Make URLs in revision comments clickable.
// @author Jason C
// @include /^https?:\/\/([^/]*\.)?stackoverflow\.com\/([^/]*\/)*revisions([\/?#].*)*$/
// @include /^https?:\/\/([^/]*\.)?serverfault\.com\/([^/]*\/)*revisions([\/?#].*)*$/
// @include /^https?:\/\/([^/]*\.)?superuser\.com\/([^/]*\/)*revisions([\/?#].*)*$/
// @include /^https?:\/\/([^/]*\.)?stackexchange\.com\/([^/]*\/)*revisions([\/?#].*)*$/
// @include /^https?:\/\/([^/]*\.)?askubuntu\.com\/([^/]*\/)*revisions([\/?#].*)*$/
// @include /^https?:\/\/([^/]*\.)?stackapps\.com\/([^/]*\/)*revisions([\/?#].*)*$/
// @include /^https?:\/\/([^/]*\.)?mathoverflow\.net\/([^/]*\/)*revisions([\/?#].*)*$/
// @grant none
// ==/UserScript==
(function() {
'use strict';
$('.revision-comment').each(function(_, c) {
c.innerHTML = linkify(c.innerText);
});
// https://stackoverflow.com/a/7123542 is good enough.
function linkify (str) {
// http://, https://, ftp://
var urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim;
// www. sans http:// or https://
var pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
// Email addresses
var emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim;
return str
.replace(urlPattern, '<a href="$&">$&</a>')
.replace(pseudoUrlPattern, '$1<a href="http://$2">$2</a>')
.replace(emailAddressPattern, '<a href="mailto:$&">$&</a>');
}
})();