chrome.runtime.onInstalled.addListener(async () => {
    let url = chrome.runtime.getURL("pages/index.html");
    let tab = await chrome.tabs.create({ url });
    console.log(`Installed Created tab ${tab.id}`);
});
// add listener for injecting content script
//chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
//  if (tab.url == "https://www.redlineshipping.com/customer/orders/print-label/*") {
//    console.log('labelfound! '+tab.url);
//  }
//})
//const REDLINE_ORIGIN = 'https://www.redlineshipping.com';

chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
  //chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    //chrome.tabs.sendMessage(tabs[0].id, {action: "open_dialog_box"}, function(response) {});  
  //});
  
  if (info.url) {
    console.log('updated: '+info.url);
    chrome.tabs.sendMessage( tabId, {
      message: 'hello!',
      url: info.url
    })
    console.log('updated '+tab.url+':'+tabId);
    console.log('content js redline listner loaded');    
  }
  
    //tab.url.replace("https://www.redlineshipping.com/customer/orders/print-label/","",function)

    //chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    //  console.log(tabs[0].url);
    //});    
  //if (!tab.url) return;
  //const url = new URL(tab.url);
  // Enables the side panel on google.com
  //if (url.origin === REDLINE_ORIGIN) {
 // 
 //chrome.sidePanel.setOptions({
  //    tabId,
  //    path: 'popup/popup.html',
  //    enabled: true
  //  });
 //       
 // } else {
  //  console.log('no match: '+url.origin);
    // Disables the side panel on all other sites
  //chrome.sidePanel.setOptions({
  //    tabId,
  //    enabled: false,
  //  });
  //}
});
chrome.tabs.onCreated.addListener(async (tab) => { 
  let url = await tab.url;
  console.log('created trigger? '+url)
  //document.addEventListener("DOMContentLoaded", function () {
    //document.querySelector("#autofill").addEventListener("click", autofillPage);  
    //console.log('created: '+tab)
    //if (changeInfo.url) {
  //chrome.tabs.sendMessage( tab, {
  //  message: 'hello back from sercvice worker!',
  //  url: tab.url
  //})
    //}
  //})
});
//chrome.declarativeNetRequest.onRuleMatchedDebug.addListener(
//  (e) => console.log("onRuleMatchedDebug" + JSON.stringify(e))
//);
/*
chrome.tabs.onUpdated.addListener(
  function(tabId, changeInfo, tab) {
    // read changeInfo data and do something with it
    // like send the new url to contentscripts.js
    console.log('updated: '+changeInfo.url);
    if (changeInfo.url) {
      chrome.tabs.sendMessage( tabId, {
        message: 'hello!',
        url: changeInfo.url
      })
    }
  }
);
*/
//chrome.sidePanel
//  .setPanelBehavior({ openPanelOnActionClick: true })
//  .catch((error) => console.error(error));
  