-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
82 lines (75 loc) · 4.67 KB
/
index.html
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
<html>
<head>
<title>Generate a bookmarklet to load docs sites at your preferred version.</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body { margin: 2em }
.site, #supported-versions { margin: 1em }
a.version, input { margin-left: .5em;}
</style>
</head>
<body>
<h1>Generate a bookmarklet to load docs sites at your preferred version.</h1>
<p>This page lets you generate a bookmarklet which will take you to your preferred version when you are on a page on the sites below. It simply detects the version in the url and updates with your chosen version.</p>
<p>For example, assume you choose version 1.9.3 for ruby-doc.org, and generate the bookmarklet. If you end up on a 2.1 doc page eg <a href="http://ruby-doc.org/core-2.1.0/Struct.html#method-i-members">http://ruby-doc.org/core-2.1.0/Struct.html#method-i-members</a>, pressing the bookmarklet will take you to <a href="http://ruby-doc.org/core-1.9.3/Struct.html#method-i-members">http://ruby-doc.org/core-1.9.3/Struct.html#method-i-members</a>.</p>
<p>1) Choose your preferred version of libraries at the following sites (or type in the version if it is not listed)</p>
<div id="sites"></div>
<p id="supported-versions">(Choose versions above)</p>
<p>2) Name your bookmarklet<input id="bookmarklet-text" type="text" value="Switch docs"/></p>
<p>3) Drag bookmarklet to bookmarks bar: <a id="bookmarkletLink">Switch docs</a></p>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
RegExp.escape = function(s) {
return s.replace(/[$-\/?[-^{|}]/g, '\\$&');
}
function recompileBookmarklet() {
$("#supported-versions").text("Chosen: " +
$.map($("input.chosen-version"), function(input, index) {
if ($(input).val()) return $(input).data('siteUrl') + ":" + $(input).val();
}).join(', ')
);
var regexesAndVersions = $.map($("input.chosen-version"), function(input, index) {
var chosenVersion = $(input).val();
if (chosenVersion) {
var compiledRegex = "/(.*" + RegExp.escape($(input).data('site-url')) + ".*)(" + $(input).data('version-regex') + ")(.*)/";
return "[" + compiledRegex + ",\"" + chosenVersion + "\"]";
}
});
if (regexesAndVersions.length > 0) {
$("#bookmarkletLink").attr("href","javascript:(function(){var v=[" + regexesAndVersions.join(',') + "];for(var i=0;i<v.length;i++){var newUrl=window.location.href.replace(v[i][0],\"$1\"+v[i][1]+\"$3\"); if (window.location.href!=newUrl)window.location.href=newUrl;}})();");
} else {
$("#bookmarkletLink").removeAttr('href');
}
}
var sites = [
{ url: "ruby-doc.org", versions: ["2.1.2", "2.1.1", "2.1.0", "2.0.0", "1.9.3", "1.9.2", "1.9.1", "1.8.7", "1.8.6"], versionRegex: /\d\.\d\.\d/ },
{ url: "rubydoc.info/stdlib", versions: ["2.1.0", "2.0.0", "1.9.3", "1.9.2", "1.8.7"], versionRegex: /\d\.\d\.\d/ },
{ url: "apidock.com/ruby", versions: ["1_9_3_392", "1_9_3_125", "1_9_2_180", "1_9_1_378", "1_8_7_330", "1_8_7_72", "1_8_6_287"], versionRegex: /\d_\d_\d_\d{1,3}/ },
{ url: "api.rubyonrails.org", versions: ["3.2.18", "3.1.3", "3.0.9", "2.3.11"], versionRegex: /\d\.\d\.\d/}
];
$.each(sites, function(index, site) {
var siteDiv = $("<div/>", {class: "site"})
$("#sites").append(siteDiv);
siteDiv.append($("<span/>", {text : site.url}));
console.dir(site.versionRegex.source);
// Yeah, size with a capital S - http://stackoverflow.com/questions/19576191/how-can-i-set-the-size-attribute-for-the-element-input-using-an-attribute-ob/19576388#19576388
var versionTextbox = $("<input/>", {type : "text", Size: "8", class: 'chosen-version', 'data-site-url': site.url, 'data-version-regex': site.versionRegex.source});
versionTextbox.change(function(e){
if (!e) var e = window.event;
recompileBookmarklet();
});
siteDiv.append(versionTextbox);
$.each(site.versions, function(index, version) {
var capturedVersionTextbox = versionTextbox;
var link = $("<a/>", {text : version, href: "#", class: "version"});
link.click(function(e) {e.preventDefault();capturedVersionTextbox.val(version);capturedVersionTextbox.change();});
siteDiv.append(link);
});
});
$('#bookmarklet-text').change(function(){
$("#bookmarkletLink").text($('#bookmarklet-text').val());
});
</script>
</body>
</html>