Repository navigation
Expand file tree
/
Copy pathselection.js
More file actions
233 lines (221 loc) · 7.13 KB
/
Copy pathselection.js
File metadata and controls
233 lines (221 loc) · 7.13 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var selectionElement; // popup appears above element being selected
var selectionText;
var stdName;
var aliases = {
"aero eng": ["aeroeng"],
astron: ["astro"],
"bio eng": ["bioe", "bio e", "bio p", "bioeng"],
biology: ["bio"],
"civ eng": ["cive", "civ e", "civeng"],
chem: ["chemistry"],
"chm eng": ["cheme"],
classic: ["classics"],
"cog sci": ["cogsci"],
colwrit: ["colwrit", "college writing"],
"com lit": ["comp lit", "complit", "comlit"],
compsci: ["cs", "comp sci", "computer science"],
"cy plan": ["cyplan", "cp"],
"des inv": ["desinv", "design"],
"dev eng": ["eveng"],
"dev std": ["devstd"],
data: ["ds"],
"ea lang": ["ealang"],
econ: ["econ", "economics"],
edstem: ["edstem", "calteach"],
"env des": ["ed"],
"el eng": ["ee", "eleng", "electrical engineering"],
eecs: ["eecs"],
"ene,res": ["erg", "er", "eneres"],
engin: ["e", "engin", "engineering"],
"env sci": ["envsci"],
"eth std": ["ethstd"],
"eura st": ["eurast"],
geog: ["geography", "geo"],
"hin-urd": ["hinurd"],
"hum bio": ["humbio"],
indones: ["indones", "indonesian"],
integbi: ["ib"],
"ind eng": ["ie", "ieor"],
japan: ["japanese"],
legalst: ["legal studies"],
linguis: ["linguistics", "ling"],
"l & s": ["l&s", "ls", "lns"],
"malay/i": ["malayi"],
"mat sci": ["matsci", "ms", "mse"],
math: ["mathematics", "maths"],
mcellbi: ["mcb"],
"mec eng": ["meceng", "meche", "mech e", "me"],
"med st": ["medst"],
"me stu": ["mestu", "middle eastern studies"],
"mil aff": ["milaff"],
"mil sci": ["milsci"],
natamst: ["native american studies", "nat am st"],
"ne stud": ["nestud"],
neurosc: ["neurosci"],
"nuc en": ["ne"],
nusctx: ["nutrisci"],
mediast: ["media"],
mongoln: ["mongolian"],
music: ["mus"],
"pb hlth": ["pbhlth", "ph", "pub hlth", "public health"],
"phys end": ["pe", "physed"],
philos: ["philo", "phil", "philosophy"],
polecon: ["poliecon"],
plantbi: ["pmb"],
"pol sci": ["poli", "polsci", "polisci", "poli sci", "ps"],
"pub pol": ["pubpol", "pp", "public policy"],
"pub aff": ["pubaff", "public affaris"],
psych: ["psychology", "psych"],
rhetor: ["rhetoric"],
"s asian": ["sasian"],
"s,seasn": ["sseasn"],
sociol: ["sociology", "socio"],
stat: ["stats"],
theater: ["tdps"],
ugba: ["haas"],
vietnms: ["vietnamese", "viet"],
"vis sci": ["vissci"],
"vis std": ["visstd"],
}; // expanded from a list in Berkeleytime
var courseRegex = /^(?:(?:[A-Z]|,){1,8}\s){1,3}?[A-Z]?\d{1,3}[A-Z]{0,2}$/;
var urlPrefix =
"https://corsproxy.io/?https://guide.berkeley.edu/ribbit/index.cgi?page=getcourse.rjs&code=";
var html;
// Convert a colloquial or unformatted name into the official format
String.prototype.getStandardName = function () {
var workingName = this.toLowerCase().replace(/\xA0/g, " "); // nbsp to space
for (var [dept, names] of Object.entries(aliases)) {
for (var name of names) {
regex = new RegExp("^" + name + "(?:\\s|(\\d))");
// ^ match aliases only when at start of string and followed by space or digit
console.log(name);
workingName = workingName.replace(regex, dept + " $1");
}
}
return workingName.toUpperCase();
};
// Set popup behavior
tippy.setDefaultProps({
maxWidth: 600,
allowHTML: true,
trigger: "manual",
onHide(instance) {
setTimeout(() => {
instance.destroy();
}, 300);
}, // delete when hidden
onShow() {
tippy.hideAll({ duration: 0 });
}, // hide any other popups
});
document.onselectionchange = () => {
// Change selection element only when text is selected
if (
!document.getSelection().isCollapsed ||
document.getSelection().type == "Range"
)
selectionElement = document.getSelection()?.anchorNode?.parentElement;
// if selection is a course name, send to background script (else false)
selectionText = document.getSelection()?.toString().trim();
if (selectionText?.getStandardName().match(courseRegex)) {
(async () => {
await chrome.runtime.sendMessage({
courseName: selectionText.getStandardName(),
});
})();
} else {
(async () => {
await chrome.runtime.sendMessage({
courseName: false,
});
})();
}
};
async function showCourseReady(response) {
// Get everything inside <course> in the API response
if ($(response).find("course").length) {
html = $($.parseXML(response)).find("course").text();
} else {
// If there's nothing try course name with a C prefix
var newResponse = await fetch(
urlPrefix + encodeURIComponent(stdName.replace(/ (\d)/, " C$1"))
);
newResponse = await newResponse.text();
if ($(newResponse).find("course").length) {
html = $($.parseXML(newResponse)).find("course").text();
} else {
// If there's still nothing try course name with a AC suffix
newResponse = await fetch(
urlPrefix + encodeURIComponent(stdName.replace(/(\d)$/, "$1AC"))
);
newResponse = await newResponse.text();
if ($(newResponse).find("course").length) {
html = $($.parseXML(newResponse)).find("course").text();
} else {
// We give up
html =
"<p>Course information cannot be found. This course may " +
"no longer be offered. If you believe there is an error or " +
"require more information, please contact the course " +
"department.</p>";
}
}
}
// Add full URLs to the links in the popup (since normally they only show on guide.berkeley.edu)
var htmlObj = $(html);
htmlObj.find("a").each(function () {
$(this).attr({
href: "https://guide.berkeley.edu" + $(this).attr("href"),
});
$(this).removeAttr("onclick");
});
html = htmlObj.prop("outerHTML");
var instance = tippy(selectionElement, {
content: html,
});
instance.show();
instance.setProps({
hideOnClick: false,
triggerTarget: document.querySelector(".tippy-box"),
onClickOutside(instance) {
instance.hide();
},
}); // popup only hides when user clicks outside of it
// When link to a course in popup is clicked, show a new popup
$(".tippy-box a.bubblelink.code").click(async function (event) {
event.preventDefault();
let stdName = $(this).attr("title").replace(/\xA0/g, " ");
try {
response = await fetch(urlPrefix + encodeURIComponent(stdName));
response = await response.text();
showCourseReady(response);
} catch (error) {
showCourseError();
}
});
}
function showCourseError() {
html = `<p>An error occurred trying to load course information.
Please try your request again later.</p>`;
tippy(selectionElement, {
content: html,
}).show();
}
// Popup is created after signal from background script when menu option clicked
chrome.runtime.onMessage.addListener(async (msg) => {
if (msg.selection) {
stdName = msg.selection.getStandardName();
selectionElement.blur();
tippy(selectionElement, {
content: "Loading course description…",
}).show();
try {
// Call API with standard course name
response = await fetch(urlPrefix + encodeURIComponent(stdName));
response = await response.text();
showCourseReady(response);
} catch (error) {
showCourseError();
}
}
});