Skip to content

Commit 83a1b0a

Browse files
committed
Performance improvements & better comments
1 parent 31611bb commit 83a1b0a

File tree

4 files changed

+112
-91
lines changed

4 files changed

+112
-91
lines changed

src/background.js

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
1-
const PRINT_URL = "my.sa.ucsb.edu/gold/UnofficialTranscript.aspx"
1+
const PRINT_HOSTNAME = "my.sa.ucsb.edu"
2+
const PRINT_PATH = "/gold/UnofficialTranscript.aspx"
23

34

45
// Called on tab updates
5-
async function tab_update_callback(tabId, changeInfo, tab) {
6-
7-
// A tab finished loading
8-
if (changeInfo.status == "complete") {
9-
10-
// The tab is a transcript print window
11-
if (String(tab.url).slice( String(tab.url).indexOf("://")+3 ).startsWith(PRINT_URL)) {
12-
console.log("Transcript Window Detected. Injecting Scripts..")
13-
14-
inject_script(tabId)
15-
}
16-
}
6+
function tab_update_callback(tabId, changeInfo, tab) {
7+
try {
8+
// Check if the tab finished loading
9+
if (changeInfo.status === "complete" && tab.url) {
10+
// Check if URL matches GOLD's unofficial transcript page
11+
const url = new URL(tab.url);
12+
if (url.hostname === PRINT_HOSTNAME && url.pathname === PRINT_PATH) {
13+
// Insert custom script & stylesheet
14+
console.info("Transcript Window Detected. Injecting Scripts...");
15+
inject_script(tabId)
16+
}
17+
}
18+
} catch (error) {
19+
console.error("Error in tab_update_callback:", error);
20+
}
1721
}
18-
19-
20-
// Inject a script into some html file
21-
async function inject_script(tabId) {
22-
chrome.scripting.executeScript(
23-
{
24-
files: ["inject.js"],
25-
target: {tabId: tabId}
26-
},
27-
insert_css.bind(null, tabId) // Pass tabId as additional argument
28-
)
22+
23+
24+
// Inject JavaScript into the transcript webpage
25+
function inject_script(tabId) {
26+
chrome.scripting.executeScript({
27+
files: ["inject.js"],
28+
target: {tabId: tabId}
29+
},
30+
(result) => {
31+
if (chrome.runtime.lastError) {
32+
console.error("JS Injection Failed (Runtime Error):", chrome.runtime.lastError);
33+
} else if (!result || result.length === 0 || result[0].result !== "ok") {
34+
console.error("JS Injection Failed (Bad Result):", result);
35+
} else {
36+
console.info("JS Injection Succeeded. Applying CSS...");
37+
insert_css(tabId);
38+
}
39+
});
2940
}
3041

3142

32-
// Apply stylesheet if JS injection was successful
33-
async function insert_css(tabId, result) {
34-
35-
// Check injection result
36-
if ((typeof result) == "object" && result.length && result[0].result == 'ok') {
37-
console.log("JS Injection Succeeded. Applying CSS..")
38-
39-
// Apply CSS
40-
chrome.scripting.insertCSS({
41-
files: ["styles.css"],
42-
target: {tabId: tabId}
43-
})
44-
}
45-
else {
46-
console.log("JS Injection Failed. Aborting..")
47-
}
43+
// Load custom CSS stylesheet
44+
function insert_css(tabId) {
45+
chrome.scripting.insertCSS({
46+
files: ["styles.css"],
47+
target: { tabId: tabId }
48+
},
49+
() => {
50+
if (chrome.runtime.lastError) {
51+
console.error("CSS Injection Failed ():", chrome.runtime.lastError);
52+
} else {
53+
console.info("CSS Injection Completed. Ready to Print!");
54+
}
55+
});
4856
}
4957

5058

src/inject.js

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
1-
// Perform any preprocessing on the html file before applying stylesheet
2-
3-
4-
// Make sure the major table spans both columns
5-
var major_row = document.querySelector("#content > table:nth-of-type(1) tr:has(> td > #pageContent_MajorList)")
6-
while (major_row.children.length) {
7-
8-
var child = major_row.children[0]
9-
var major_list = child.querySelector("#pageContent_MajorList")
10-
11-
if (major_list) {
12-
child.setAttribute("colspan", 2)
13-
child.classList.add("major-table")
14-
15-
major_row.innerHTML = child.outerHTML
16-
break;
17-
}
18-
19-
major_row.removeChild(child)
1+
// Pre-process the html file before applying stylesheet
2+
3+
function main() {
4+
// Find the table that contains degree information
5+
// In GOLD, this table doesn't span the width of the screen, even though it should
6+
const major_list = document.querySelector("#pageContent_MajorList");
7+
if (!major_list) { return "Didn't find element with ID #pageContent_MajorList"; }
8+
9+
// Find the parent cell of the table
10+
const parent_cell = major_list.closest("td");
11+
if (!parent_cell) { return "Couldn't find parent cell of major list"; }
12+
13+
// Modify attribute to allow cell to span row
14+
parent_cell.setAttribute("colspan", "2");
15+
16+
// Find the parent row of this cell
17+
const parent_row = parent_cell.closest("tr");
18+
if (!parent_row) { return "Couldn't find parent row of major list"; }
19+
20+
// Wipe row contents and re-insert the cell we care about
21+
parent_row.innerHTML = "";
22+
parent_row.appendChild(parent_cell);
23+
24+
25+
// Replace print buttons
26+
// Printing usually opens a pop-out, but we just want to print the page as-is
27+
const nav_div = document.querySelector("#pageContent_titleNavDiv > div:not(.title)")
28+
if (!nav_div) { return "Couldn't find div containing print buttons"; }
29+
nav_div.innerHTML = "<input type=button onclick=window.print() class=gold-button id=print-button value='Print Transcript' />";
30+
31+
32+
// Add an ID to timestamp cell for efficient CSS selection
33+
const date_text = document.querySelector("#pageContent_currentDateLabel");
34+
if (!date_text) { return "Couldn't find timestamp text"; }
35+
const date_cell = date_text.closest("td");
36+
if (!date_cell) { return "Couldn't find parent cell of timestamp text"; }
37+
date_cell.id = "timestampCell"
38+
39+
return "ok";
2040
}
2141

22-
23-
// Replace print buttons to not create popout window
24-
var nav_div = document.querySelector("#pageContent_titleNavDiv > div:has(> .gold-button)")
25-
if (nav_div) {
26-
nav_div.innerHTML = "<input type=button onclick=window.print() class=gold-button id=print-button value='Print Transcript' />"
42+
try {
43+
main(); // Invoke main function
44+
} catch(error) {
45+
console.error("Error in inject.js", error);
2746
}
28-
29-
30-
"ok" // This will be returned to callback in background.js

src/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"manifest_version": 3,
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"name": "GOLD Transcript Printer",
55
"description": "Produces a better-looking output when printing unofficial transcripts from GOLD",
66
"icons": {

src/styles.css

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
1-
/* Define Style Options for Printing */
1+
/* Style options specifically for printing */
22
@media print {
33

4-
#MainForm *:not(#content):not(#content *) {
4+
/* Hide everything that isn't inside the content div */
5+
#MainForm > :not(#content) {
56
display: none!important;
67
}
78

8-
.gold-button {
9+
/* Remove title and welcome message */
10+
#MainForm > #content > :not(.row) {
911
display: none!important;
1012
}
11-
13+
14+
/* Hide the nav bar */
1215
#pageContent_titleNavDiv {
1316
display: none!important;
1417
}
1518

16-
#userLabel {
17-
display: none!important;
18-
}
19-
19+
/* Remove extra whitespace */
2020
#content {
21+
margin:0!important;
2122
padding:0!important;
2223
}
23-
24-
#content > table:nth-of-type(1) td:has(> #pageContent_currentDateLabel ) {
25-
width: 50%!important;
26-
}
27-
28-
#pageContent_currentDateLabel {
29-
float: right!important;
30-
}
24+
}
3125

32-
.page-title {
33-
padding-left: 0!important;
34-
}
26+
/* Always applied */
27+
/* Prevent "University of California, Santa Barbara" text from wrapping*/
28+
#timestampCell {
29+
width: 50%;
30+
}
3531

36-
tr:has(> .label) {
37-
display: none!important;
38-
}
32+
/* Right-justify timestamp */
33+
#pageContent_currentDateLabel {
34+
float: right;
3935
}
4036

41-
/* Always Applied */
37+
/* Add some space before degree information */
4238
#pageContent_MajorList {
4339
margin-top: 20px;
4440
}
4541

42+
/* Make print button look pretty */
4643
#print-button {
4744
padding: 10px 20px;
4845
}

0 commit comments

Comments
 (0)