-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageidsearch.js
128 lines (112 loc) · 4.85 KB
/
pageidsearch.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
// Retrieves the xID found on UNR pages (the manifest controls page access).
var bodyID = document.getElementsByTagName("body")[0].id;
// This is an array which stores keypresses.
var keys = [];
// retrieves the header of pages so that they are available for parsing
var currentLocation = window.location.pathname.toString().toLowerCase();
// if it includes NevadaToday, set the current location to NevadaToday. This is to make sure all URLS that contain nevada-today are captured by the slow XID parser.
if(currentLocation.includes("/nevada-today"))
{
currentLocation = "/nevada-today";
}
// true or false booleans
var boolNevadaToday = false;
// checks to see whether or not we're on most unr.edu pages except the one with nevada today.
mainController(currentLocation);
// checks to see if on a nevadatoday page and fills fullHeader with a massive string - this makes the addon more responsive on pages that don't require it.
boolNevadaToday = nevadaTodayController(currentLocation);
// Controllers
function mainController(currentLocation)
{
// As long as the current location isn't at nevada-today, we will load the event handlers that trigger the popup.
if(currentLocation != "/nevada-today")
{
window.addEventListener("keydown", keysPressed, false);
window.addEventListener("keyup", keysReleased, false);
}
}
// Triggered upon loading nevadaToday, so that the slower pageID method is called
function nevadaTodayController(currentLocation)
{
if(currentLocation==="/nevada-today")
{
var fullHeader = document.getElementsByTagName("head")[0].innerHTML;
// once retrieved, this parses the fullHeader to fetch the PageID:
var newBodyID = slowXIDFetch(fullHeader);
bodyID = newBodyID;
window.addEventListener("keydown", keysPressed, false);
window.addEventListener("keyup", keysReleased, false);
}
}
// Trigger copy to clipboard upon hotkey.
function copyToClipboard(bodyID)
{
window.prompt("Copy to clipboard: Ctrl+C, Enter", bodyID);
}
// Controls actions upon keypresses. Takes global arguments.
function keysPressed(e)
{
// store an entry for every key pressed
keys[e.keyCode] = true;
// Ctrl + Shift + Z
if(keys[17] && keys[16] && keys[90])
{
// do something - interestingly enough, this works without needing to be called outside of the function.
// if on a normal UNR page
copyToClipboard(bodyID);
// prevent default browser behavior
e.preventDefault();
// this "releases" the key combination, so that there isnt an infinite loop of prompts
// this is used instead of the keysReleased function because we can specify multiple keys at once, versus last used.
keys[17] = false; // turns ctrl back to normal
keys[16] = false; // turns shift back to normal
keys[90] = false; // turns z back to normal
}
}
// If you're feeling lazy and just want to turn off the last used key.
function keysReleased(e)
{
// mark keys that were released
keys[e.keyCode] = false;
}
// Function that parses a header in HTML and returns an XID.
function slowXIDFetch(fullHeader)
{
var fullHeaderLength = fullHeader.length;
// unicode literal for the spacebar space - see http://stackoverflow.com/questions/19810122/how-do-i-add-a-non-breaking-whitespace-in-javascript-without-using-innerhtml
var endOfXID = '.';
// go through element by element within the comment's head
for(var i=0;i<fullHeaderLength;i++)
{
// This fetches the pageID from a comment in the header. It is really inefficient and it's really ugly - but it works.
if(fullHeader[i] == "P" && fullHeader[i+1] == "a" && fullHeader[i+2] == "g" && fullHeader[i+3] == "e" && fullHeader[i+4] == "I" && fullHeader[i+5] == "D" && fullHeader[i+6] == ":")
{
// i+7 is whitespace
// thus, we need to fetch the xID until the next whitespace
var start = i+7;
// i+8 is the first character, which is the x. hense why we're adding a count of 1.
var count = 1;
// this is the position of where the characters are added to the array.
var arraySize = 0;
// this is the array that stores the xid
var slowPageID = [];
// this is the string for slowPageID
var stringSlowPageID;
// we'll count up until we hit endOfXID
// while the elements in the array do not match the endOfXID character (which is a period)
while(fullHeader[start+count] != endOfXID)
{
// log what is stored at that element
slowPageID[arraySize] = fullHeader[start+count];
// keep counting up until we hit endOfXID
count++;
// move the arraysize up by one to store the next character there.
arraySize++;
}
// turns the array of character into a string and also removes the commas
stringSlowPageID = slowPageID.join('');
// then return stringSlowPageID so that it's the new bodyID
return stringSlowPageID;
}
}
}