@@ -4,39 +4,37 @@ export const githubURL = 'https://github.com/cssnr/link-extractor'
44
55/**
66 * Inject extract.js to Tab and Open links.html with params
7- * @function processLinks
7+ * @function injectTab
88 * @param {Object } injectOptions Inject Tab Options
99 * @param {String } [injectOptions.filter] Regex Filter
1010 * @param {Boolean } [injectOptions.domains] Only Domains
1111 * @param {Boolean } [injectOptions.selection] Only Selection
12+ * @param {Boolean } [injectOptions.open] Open Links Page
13+ * @param {chrome.tabs.Tab } [injectOptions.tab] Open Links Page
1214 * @return {Promise<void> }
1315 */
1416export async function injectTab ( {
1517 filter = null ,
1618 domains = false ,
1719 selection = false ,
20+ open = true ,
21+ tab = null ,
1822} = { } ) {
1923 console . log ( 'injectTab:' , filter , domains , selection )
2024
2125 // Extract tabIds from all highlighted tabs
2226 const tabIds = [ ]
23- const tabs = await chrome . tabs . query ( {
24- currentWindow : true ,
25- highlighted : true ,
26- } )
27- if ( ! tabs . length ) {
28- const [ tab ] = await chrome . tabs . query ( {
29- currentWindow : true ,
30- active : true ,
31- } )
32- console . debug ( `tab: ${ tab . id } ` , tab )
27+ if ( tab ) {
3328 tabIds . push ( tab . id )
3429 } else {
30+ const tabs = await chrome . tabs . query ( {
31+ currentWindow : true ,
32+ highlighted : true ,
33+ } )
34+ console . debug ( 'tabs:' , tabs )
3535 for ( const tab of tabs ) {
3636 console . debug ( `tab: ${ tab . id } ` , tab )
37- // tab.url undefined means we do not have permissions on this tab
3837 if ( ! tab . url ) {
39- // chrome.runtime.openOptionsPage()
4038 const url = new URL (
4139 chrome . runtime . getURL ( '/html/permissions.html' )
4240 )
@@ -49,15 +47,28 @@ export async function injectTab({
4947 tabIds . push ( tab . id )
5048 }
5149 }
50+ console . log ( 'tabIds:' , tabIds )
5251 if ( ! tabIds . length ) {
53- console . log ( '%cNo Tab IDs to Inject' , 'color: Yellow' )
52+ // TODO: Display Error to User
53+ console . error ( 'No Tab IDs to Inject' )
5454 return
5555 }
56- console . log ( 'tabIds:' , tabIds )
5756
58- // Create URL to links.html
59- const url = new URL ( chrome . runtime . getURL ( '/html/links.html' ) )
57+ // Inject extract.js which listens for messages
58+ for ( const tab of tabIds ) {
59+ console . debug ( `injecting tab.id: ${ tab } ` )
60+ await chrome . scripting . executeScript ( {
61+ target : { tabId : tab } ,
62+ files : [ '/js/extract.js' ] ,
63+ } )
64+ }
6065
66+ // Create URL to links.html if open
67+ if ( ! open ) {
68+ console . debug ( 'Skipping opening links.html on !open:' , open )
69+ return
70+ }
71+ const url = new URL ( chrome . runtime . getURL ( '/html/links.html' ) )
6172 // Set URL searchParams
6273 url . searchParams . set ( 'tabs' , tabIds . join ( ',' ) )
6374 if ( filter ) {
@@ -69,16 +80,6 @@ export async function injectTab({
6980 if ( selection ) {
7081 url . searchParams . set ( 'selection' , selection . toString ( ) )
7182 }
72-
73- // Inject extract.js which listens for messages
74- for ( const tab of tabIds ) {
75- console . debug ( `injecting tab.id: ${ tab } ` )
76- await chrome . scripting . executeScript ( {
77- target : { tabId : tab } ,
78- files : [ '/js/extract.js' ] ,
79- } )
80- }
81-
8283 // Open Tab to links.html with desired params
8384 console . debug ( `url: ${ url . href } ` )
8485 await chrome . tabs . create ( { active : true , url : url . href } )
@@ -394,3 +395,18 @@ export function detectBrowser() {
394395 }
395396 return browser
396397}
398+
399+ /**
400+ * @function updateBrowser
401+ * @return {Promise<void> }
402+ */
403+ export function updateBrowser ( ) {
404+ let selector = '.chrome'
405+ // noinspection JSUnresolvedReference
406+ if ( typeof browser !== 'undefined' ) {
407+ selector = '.firefox'
408+ }
409+ document
410+ . querySelectorAll ( selector )
411+ . forEach ( ( el ) => el . classList . remove ( 'd-none' ) )
412+ }
0 commit comments