-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
127 lines (121 loc) · 3.91 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bookmarklet generator</title>
<style>
fieldset input, output {
display: block;
}
form, #instructions {
float: left;
width: 45%;
padding: 5px;
}
#explanations {
border-left: 2px solid #ccc;
}
[hidden=true] {
display: none;
}
</style>
</head>
<h1>Bookmarklet generator</h1>
<p>Switch between different versions of a website (production, stage, working directory, etc)</p>
<form>
<label>Bookmark title: <input id="bookmark-title" placeholder="Switch example.com"></label>
<fieldset>
<legend>Domains</legend>
<input class="domain" placeholder="www.example.com">
<input class="domain" placeholder="stage.example.com">
<input class="domain" placeholder="example.local">
<button type="button">+</button>
</fieldset>
<input type="submit" value="Generate boorkmarklet">
<output hidden="true">Drag me or copy me: <a href="#"></a></output>
</form>
<div id="instructions">
<p>This will generate a bookmarklet that looks like this:</p>
<ul style="display: inline-block; z-index: 1000; background: white; color: black; border: 2px solid black; padding: 5px;">
<li style="display: block;">
<a href="#">www.example.com</a>
</li>
<li style="display: block;">
<a href="#">stage.example.com</a>
</li>
<li style="display: block;">
<a href="#">example.local</a>
</li>
</ul>
<p>It will take you to the exact same page on another domain.</p>
<p>If you have urls in the form <em>user.domain.com</em>, it can switch between <em>user.domain.com</em> and <em>user.stage.domain.com</em>. Just enter <em>domain.com</em> and <em>stage.domain.com</em>. </p>
</div>
<script>
function generateBookarklet(e) {
e.preventDefault();
// get all input,
var domains = [];
var domainsInput = document.querySelectorAll('input.domain');
for (var i=0, il=domainsInput.length; i<il; i++) {
var domain = domainsInput[i].value;
if (domain.length > 0) {
domains.push('"' + domain + '"');
}
}
domainArray= '[' + domains.join(',') + ']';
// generate bookmark string
var bookmarklet = 'javascript:(function(){var domains=' + domainArray + ', \
loc=window.location, \
path=loc.pathname+loc.search+loc.hash, \
url_prefix="http://"; \
\
for(var i=0;i<domains.length;i++){ \
var index_host=loc.href.indexOf(domains[i]); \
if(index_host>=0){ \
domains.splice(i,1); \
url_prefix=loc.href.slice(0,index_host); \
} \
} \
/*if(!c){ \
return \
}*/ \
var ul=document.createElement("ul"); \
ul.style.position="fixed"; \
ul.style.zIndex=1000; \
ul.style.background="#fff"; \
ul.style.color="#000"; \
ul.style.border="2px solid #000"; \
ul.style.top="0"; \
ul.style.left="0"; \
ul.style.padding="5px"; \
for(var i=0;i<domains.length;i++){ \
var a=document.createElement("a"); \
a.href=url_prefix+domains[i]+path; \
a.appendChild(document.createTextNode(domains[i])); \
var li=document.createElement("li"); \
li.style.display="block"; \
li.appendChild(a); \
ul.appendChild(li) \
} \
document.body.appendChild(ul) \
})();';
// generate link to copy.
var output = document.querySelector('output');
var a = output.querySelector('a');
a.href = bookmarklet;
var title = document.getElementById('bookmark-title').value;
if (title.length == 0) {
title = "Switch servers";
};
a.innerHTML = title;
output.removeAttribute('hidden');
}
function addField(e) {
var input = document.createElement('input');
input.className = "domain";
input.placeholder = "another.domain.com";
e.target.parentNode.insertBefore(input, e.target);
}
document.querySelector('form').addEventListener('submit', generateBookarklet, false);
document.querySelector('button').addEventListener('click', addField, false);
</script>