-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtil.js
88 lines (70 loc) · 2.56 KB
/
Util.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
function AddTextToTextArea(ta, text) {
const start = ta.selectionStart;
const end = ta.selectionEnd;
if (start || start === 0) {
ta.value = ta.value.substring(0, start)
+ text
+ ta.value.substring(end, ta.value.length);
}
else {
ta.value += text;
}
// Set the cursor at the end of the new style tag
ta.focus();
ta.setSelectionRange(start + text.length, start + text.length);
}
function CreateStyleTagButton(textarea, buttonName, buttonType) {
const newButton = document.createElement('button');
newButton.id = `${buttonName}Button`;
newButton.type = 'button';
newButton.innerText = buttonType;
newButton.tabIndex = -1;
newButton.onclick = function() {
AddTextToTextArea(textarea, newButton.innerText.startsWith('/') ? `</${buttonType}>` : `<${buttonType}>`);
console.log(newButton.innerText.startsWith('/'));
newButton.innerText = newButton.innerText.startsWith('/') ? `${buttonType}` : `/${buttonType}`;
};
return newButton;
}
function GetBackgroundColor() {
return window.getComputedStyle(document.querySelector('.message-top'), null).backgroundColor;
}
function GetPageNumber() {
const pageNumber = GetUrlParameter('page');
return pageNumber === null ? 1 : parseInt(pageNumber);
}
function GetParameter(url, key) {
const decodeUrl = decodeURIComponent(url);
let value = null;
decodeUrl.split('&').forEach(function (queryString) {
const urlParameter = queryString.split('=');
if (urlParameter[0] === key)
value = urlParameter[1];
});
return value;
}
// key = url parameter
// Examples:
// id = Message ID (used with message.php)
// page = Page Number (not present on page 1)
// r = Revision Number (for individual messages)
// u = User ID (Only used on showmessages.php in conjunction with topic for filtering)
function GetUrlParameter(key) {
return GetParameter(window.location.search.substring(1), key);
}
function GetUrlTopic() {
return window.location.toString().split('&')[0];
}
function GetUserId() {
const userbar = document.querySelector('.userbar');
const url = userbar.querySelector('a').getAttribute('href');
return GetParameter(url.split('?')[1], 'user');
}
function GetUserIdFromPost(post) {
const url = post.querySelector('.message-top a').getAttribute('href');
return GetParameter(url.split('?')[1], 'user');
}
function GetUsernameFromPost(post) {
const username = post.querySelector('.message-top a').innerHTML;
return username === 'Filter' ? null : username;
}