var matching = document.querySelector('#matching-url');
var not_pinned = document.querySelector('#not-pinned');
var case_sensitive = document.querySelector('#case-sensitive');
var button = document.querySelector('button');

// set to true to skip closing - just print what we'd do
var debug_mode = false;

// set default and update value
button.disabled = !matching.value;
matching.addEventListener("input", (e) => {
  button.disabled = !matching.value;
});

// switch tabs on select
var tabs = document.querySelectorAll('.panel-section-tabs-button');
tabs.forEach((this_tab) => { this_tab.addEventListener("click", (e) => {
  tabs.forEach((tab) => {
    tab.classList.remove("selected");
    tab.classList.remove("select"); // temporary workaround
    document.querySelector("#" + tab.getAttribute("target")).style.display="none";
  });
  this_tab.classList.add("selected");
  this_tab.classList.add("select"); // temporary workaround
  document.querySelector("#" + this_tab.getAttribute("target")).style.display="inline";
})});

button.addEventListener("click", (e) => {
  let match = matching.value;
  if (match) {
    let n_pinned = not_pinned.checked;
    let by_title = document.querySelector('#title-form-tab').classList.contains("selected");
    let sensitive = by_title ? case_sensitive.checked : true;

    if (!sensitive)
      match = match.toLowerCase();

    // get current window with tabs
    browser.windows.getCurrent({populate: true}).then((window) => {

      for (var i = 0; i < window.tabs.length; i++) {
        let t = window.tabs[i];
        let val = by_title ? t.title : t.url; // TODO - title should be case insensitive

        if (!sensitive)
          val = val.toLowerCase();

        if (val.includes(match)) {
          if (!n_pinned || !t.pinned) {
            if (debug_mode)
              console.log("closed tab: " + t.url);
            else
              browser.tabs.remove(t.id);
          }
        }
      }
    });
  }
});

@@ -1028,299 +1028,75 @@
 );%0A%0A
-button.addEventListener(%22click%22, (e) =%3E %7B%0A  let match = matching.value;%0A  if (match
+function match_tabs(args
 ) %7B%0A
+%0A
-  
   let 
-n_pinned = not_pinned.checked;%0A    let by_title = document.querySelector('#title-form-tab').classList.contains(%22selected%22);%0A    let sensitive = by_title ? case_sensitive.checked : true;%0A%0A  
+match = args.match;%0A
   if (!
+args.
 sens
@@ -1098,26 +1098,24 @@
 .sensitive)%0A
-  
     match = 
@@ -1138,18 +1138,16 @@
 e();%0A%0A  
- 
- 
 // get c
@@ -1172,17 +1172,22 @@
  tabs%0A  
- 
+return
  browser
@@ -1242,24 +1242,46 @@
 ow) =%3E %7B%0A%0A  
+  let matched = %5B%5D;%0A %0A
     for (var
@@ -1321,26 +1321,24 @@
 ++) %7B%0A      
-  
 let t = wind
@@ -1351,26 +1351,24 @@
 s%5Bi%5D;%0A      
-  
 let val = by
@@ -1365,16 +1365,21 @@
 t val = 
+args.
 by_title
@@ -1448,23 +1448,26 @@
 %0A%0A      
-  
 if (!
+args.
 sensitiv
@@ -1469,18 +1469,16 @@
 sitive)%0A
-  
         
@@ -1503,18 +1503,16 @@
 ase();%0A%0A
-  
       if
@@ -1544,23 +1544,26 @@
         
-  
 if (!
+args.
 n_pinned
@@ -1585,24 +1585,604 @@
 %7B%0A          
+matched.push(t);%0A        %7D%0A      %7D%0A    %7D%0A    return matched;%0A  %7D);%0A%7D%0A%0A// get args from form%0Afunction get_args() %7B%0A  let by_title = document.querySelector('#title-form-tab').classList.contains(%22selected%22);%0A  return %7B%0A    match: matching.value,%0A    n_pinned: not_pinned.checked,%0A    by_title: by_title,%0A    sensitive: by_title ? case_sensitive.checked : true%0A  %7D;%0A%7D%0A%0A// the main closer%0Afunction close_matched() %7B%0A  let args = get_args();%0A  if (args.match) %7B%0A    match_tabs(args).then((matched) =%3E %7B%0A      for (var i = 0; i %3C matched.length; ++i) %7B%0A        let t = matched%5Bi%5D;%0A      
   if (debug_
@@ -2187,20 +2187,16 @@
 g_mode)%0A
-    
         
@@ -2246,21 +2246,13 @@
     
-    
 else%0A
-    
     
@@ -2294,43 +2294,85 @@
     
+%7D%0A
     %7D
+);
 %0A  
-      %7D%0A      %7D%0A    %7D);%0A  %7D
+%7D%0A%7D%0A%0Abutton.addEventListener(%22click%22, (e) =%3E %7B%0A  close_matched();
 %0A%7D);

refactor main calls to prepare for new features