|
| 1 | + |
| 2 | +// TODO: consolated csp.js and on_install.js into single background.js file |
| 3 | + |
| 4 | +// functions for refreshing tabs after an extension install: |
1 | 5 | function find_these (query) {
|
2 | 6 | return new Promise((resolve)=>{
|
3 | 7 | if (Object.keys(query).includes('title')) {
|
@@ -55,3 +59,39 @@ function when_installed ({reason}) {
|
55 | 59 | }
|
56 | 60 |
|
57 | 61 | chrome.runtime.onInstalled.addListener(when_installed);
|
| 62 | + |
| 63 | +// functions for handling auth changes: |
| 64 | + |
| 65 | +const auth_header ='x-codesy-auth-changed' |
| 66 | +const has_auth_header = ({name})=>{ return name.startsWith(auth_header) } |
| 67 | + |
| 68 | +function check_codesy_auth ({responseHeaders}) { |
| 69 | + auth_headers = responseHeaders.filter(has_auth_header) |
| 70 | + if ( auth_headers.length > 0 ) { |
| 71 | + find_these( {url: "*://*.github.com/*"} ) |
| 72 | + .then(reload_them) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function listen_for_auth (domain) { |
| 77 | + const filter = {urls:[`${domain}/*`], types:["main_frame"]}; |
| 78 | + const options = ["responseHeaders"] |
| 79 | + if (chrome.webRequest.onHeadersReceived.hasListener(check_codesy_auth)) { |
| 80 | + chrome.webRequest.onHeadersReceived.removeListener(check_codesy_auth); |
| 81 | + } |
| 82 | + chrome.webRequest.onHeadersReceived.addListener( |
| 83 | + check_codesy_auth, filter, options |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +chrome.storage.local.get(null, |
| 88 | + ({domain = "https://www.codesy.io" })=>{ |
| 89 | + if (domain) listen_for_auth(domain); |
| 90 | + } |
| 91 | +); |
| 92 | + |
| 93 | +chrome.storage.onChanged.addListener( |
| 94 | + ({domain: {newValue: domain} }, namespace)=>{ |
| 95 | + if (domain) listen_for_auth(domain); |
| 96 | + } |
| 97 | +); |
0 commit comments