-
Notifications
You must be signed in to change notification settings - Fork 0
/
Site.js
219 lines (186 loc) · 6.52 KB
/
Site.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
document.addEventListener('DOMContentLoaded', () => {
Site.setup();
});
class Site extends DataComponent {
static documentElementId = 'sites-mb';
static listId = 'sites-mb';
static listpadType = 'sitepad';
static dataKey = 'siteData';
static AUTOMATIC = 'Automatic';
static MANUAL = 'Manual';
static setup() {
this.requestUrl();
super.setup();
}
static documentElementToLocalStorageObject() {
let sites = [];
[...document.getElementsByClassName(this.listpadType)].forEach(listpad => {
sites.push({
title: listpad.getElementsByClassName('sitepad-title')[0].value,
url: listpad.getElementsByClassName('sitepad-url')[0].value,
instructions: listpad.getElementsByClassName('sitepad-instructions')[0].value,
active: listpad.getElementsByClassName('sitepad-active')[0].innerText == this.AUTOMATIC,
uid: listpad.getAttribute('uid')
});
});
let data = this.initialize();
data.siteData.sites = sites;
return data;
}
static localStorageObjectToDocumentElement(data) {
this.list().createNewListpadButton('New Site', () => {
this.prependSiteToPopup(this.createBlankSiteObject());
this.updateDataFromPopup();
});
let sites = data.siteData.sites;
sites.forEach(site => {
this.documentElement().append(this.createSite(site));
});
return this.documentElement();
}
static prependSiteToPopup(site) {
this.prependListpad(this.createSite(site));
}
static addSiteToPopup(site) {
this.documentElement().append(this.createSite(site));
}
static createSite(site) {
let listpad = this.createListpad(site.uid);
let listbox = listpad.listbox;
let title = listbox.createElement('input', 'title');
title.placeholder = 'description';
title.value = site.title;
let url = listbox.createElement('input', 'url');
url.placeholder = 'site';
url.value = site.url;
let instructions = listbox.createElement('textarea', 'instructions');
instructions.className += ' code faux-console';
instructions.placeholder = 'Site instructions';
instructions.value = site.instructions;
instructions.spellcheck = false;
let reloadBtn = listbox.createElement('button', 'reload', [{'noUpdate':true}]);
reloadBtn.innerText = 'Reload';
reloadBtn.onclick = (() => {
this.runInstructions(instructions.value);
});
let activeBtn = listbox.createElement('button', 'active');
activeBtn.innerText = site.active ? this.AUTOMATIC : this.MANUAL;
activeBtn.addEventListener('click', (() => {
activeBtn.innerText = activeBtn.innerText === this.AUTOMATIC ? this.MANUAL : this.AUTOMATIC;
}));
return listpad.element();
}
static initialize() {
return {
siteData: {
sites: [
]
}
};
}
static createBlankSiteObject() {
return {
title: '',
url: this.getSimpleUrl(),
instructions: '',
active: false,
uid: createUid()
}
}
static requestUrl() {
Popup.sendMessageToTab({
title: 'SEND_URL'
});
}
static setUrl(url) {
this.prependMatching(url);
let simpleUrl = this.simplifyUrl(url);
this.list().getListpads().forEach(listpad => {
let urlInput = listpad.getElementsByClassName('sitepad-url')[0];
urlInput.setAttribute('placeholder', simpleUrl);
if (!urlInput.value) {
urlInput.value = simpleUrl;
}
});
this.list().newListpadButton().setAttribute('current-site', url);
}
static getUrl() {
return this.list().newListpadButton().getAttribute('current-site');
}
static getSimpleUrl() {
return this.simplifyUrl(this.getUrl());
}
static simplifyUrl(url) {
if (!url) {
return '';
}
let ignoreStarts = ['https://', 'http://', 'www.'];
for (let i = 0; i < ignoreStarts.length; i++) {
let ignoreStart = ignoreStarts[i];
if (url.includes(ignoreStart)) {
url = url.substr(url.indexOf(ignoreStart) + ignoreStart.length);
}
}
let ignoreEnds = ['/', '?', '#', '&'];
for (let i = 0; i < ignoreEnds.length; i++) {
let ignoreEnd = ignoreEnds[i];
if (url.includes(ignoreEnd)) {
url = url.substr(0, url.indexOf(ignoreEnd));
}
}
url = url.toLowerCase();
return url;
}
static runInstructions(instructions) {
Code.sendCodeToTab(instructions);
}
static prependMatching(url) {
this.getData((data) => {
let sites = data.siteData.sites;
sites.forEach(site => {
if (this.urlMatches(site.url, url)) {
this.prependListpad(this.list().getListpad(site.uid));
}
})
});
}
// copy+pasted form Tab.js
static urlMatches(savedUrl, currentUrl) {
// multiple strings
let seperateUrls = savedUrl.split(',');
if (seperateUrls.length > 1) {
for (let i = 0; i < seperateUrls.length; i++) {
if (this.urlMatches(seperateUrls[i].trim(), currentUrl)) {
return true;
}
}
return;
}
// exact string
if (savedUrl.includes('"')) {
return currentUrl == savedUrl.replaceAll('"', '');
}
// includes string
let splitUrlSections = savedUrl.split('*');
for (let i = 0; i < splitUrlSections.length; i++) {
if (!currentUrl.includes(splitUrlSections[i])) {
return;
}
currentUrl = currentUrl.substring(currentUrl.indexOf(splitUrlSections[i]) + splitUrlSections[i].length);
}
return true;
}
static sendInstructionsMatchingUrl(url) {
url = this.simplifyUrl(url);
this.getData((data) => {
let sites = data.siteData.sites;
sites.forEach(site => {
if (site.url == url) {
if (site.active) {
this.runInstructions(site.instructions);
}
}
})
});
}
}