| 697 | let unregister; |
| 698 | |
| 699 | export async function openAuthPage(url, redirectUri) { |
| 700 | unregister?.(); // otherwise our new tabId will be ignored |
| 701 | const tabId = (await browser.tabs.create({ url })).id; |
| 702 | /** |
| 703 | * @param {chrome.webRequest.WebResponseDetails} info |
| 704 | * @returns {chrome.webRequest.BlockingResponse} |
| 705 | */ |
| 706 | const handler = (info) => { |
| 707 | if (getService().checkAuth?.(info.url)) { |
| 708 | // When onBeforeRequest occurs for initial requests intercepted by service worker, |
| 709 | // info.tabId will be -1 on Chromium based browsers, use tabId instead. |
| 710 | // tested on Chrome / Edge / Brave |
| 711 | browser.tabs.remove(tabId); |
| 712 | // If we unregister without setTimeout, API will ignore { cancel: true } |
| 713 | setTimeout(unregister, 0); |
| 714 | return { cancel: true }; |
| 715 | } |
| 716 | }; |
| 717 | unregister = () => { |
| 718 | browser.webRequest.onBeforeRequest.removeListener(handler); |
| 719 | }; |
| 720 | // Note: match pattern does not support port number |
| 721 | // - In Chrome, the port number is ignored and the pattern still works |
| 722 | // - In Firefox, the pattern is ignored and won't match any URL |
| 723 | redirectUri = redirectUri.replace(/:\d+/, ''); |
| 724 | browser.webRequest.onBeforeRequest.addListener( |
| 725 | handler, |
| 726 | { |
| 727 | // Do not filter by tabId here, see above |
| 728 | urls: [`${redirectUri}*`], |
| 729 | types: ['main_frame', 'xmlhttprequest'], // fetch request in service worker |
| 730 | }, |
| 731 | ['blocking'], |
| 732 | ); |
| 733 | } |
| 734 | |
| 735 | const base64urlMapping = { |
| 736 | '+': '-', |