|
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" |
2 | 3 |
|
3 | 4 |
|
4 | 5 | // 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 | + } |
17 | 21 | } |
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 | + }); |
29 | 40 | } |
30 | 41 |
|
31 | 42 |
|
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 | + }); |
48 | 56 | } |
49 | 57 |
|
50 | 58 |
|
|
0 commit comments