@@ -53,9 +53,14 @@ let currentScanFilter = FILTER_OPTIONS.BOTH;
5353function normalizeFilter ( value ) {
5454 return Object . values ( FILTER_OPTIONS ) . includes ( value ) ? value : FILTER_OPTIONS . BOTH ;
5555}
56- try { chrome . action . setPopup ( { popup : 'popup.html' } ) ; } catch ( err ) { console . warn ( 'Popup attach failed:' , err ) ; }
56+ try {
57+ chrome . action . setPopup ( { popup : 'popup.html' } ) ;
58+ } catch ( err ) {
59+ console . warn ( 'Popup attach failed:' , err ) ;
60+ }
5761
58- let originTabId ; // Store the ID of the tab that initiated the license check
62+ // Store the ID of the tab that initiated the license check
63+ let originTabId ;
5964const dmp = new DiffMatchPatch ( ) ;
6065// In-memory cache of license term-frequency vectors (populated on demand)
6166let licenseVectorsCache = null ; // { license_key: { word: freq, ... } }
@@ -65,13 +70,17 @@ const activeScans = new Set();
6570
6671// Database version - increment when structure changes
6772const DB_VERSION = 2 ;
68-
73+ let dbInstance = null ;
74+ let dbOpenPromise = null ;
6975/**
7076 * Open (and create/upgrade) the IndexedDB database.
7177 * @returns {Promise<IDBDatabase> }
7278 */
7379function openDatabase ( ) {
74- return new Promise ( ( resolve , reject ) => {
80+ if ( dbInstance ) return Promise . resolve ( dbInstance ) ;
81+ if ( dbOpenPromise ) return dbOpenPromise ;
82+
83+ dbOpenPromise = new Promise ( ( resolve , reject ) => {
7584 const request = indexedDB . open ( 'LicenseDB' , DB_VERSION ) ;
7685
7786 request . onupgradeneeded = ( event ) => {
@@ -91,13 +100,23 @@ function openDatabase() {
91100 } ;
92101
93102 request . onsuccess = ( event ) => {
94- resolve ( event . target . result ) ;
103+ dbInstance = event . target . result ;
104+ dbInstance . onclose = ( ) => { dbInstance = null ; dbOpenPromise = null ; } ;
105+ dbInstance . onversionchange = ( ) => {
106+ dbInstance ?. close ( ) ;
107+ dbInstance = null ;
108+ dbOpenPromise = null ;
109+ } ;
110+ resolve ( dbInstance ) ;
95111 } ;
96112
97113 request . onerror = ( event ) => {
114+ dbOpenPromise = null ;
98115 reject ( `IndexedDB error: ${ event . target . errorCode } ` ) ;
99116 } ;
100117 } ) ;
118+
119+ return dbOpenPromise ;
101120}
102121
103122/**
@@ -504,7 +523,7 @@ async function loadAllVectors() {
504523 return licenseVectorsCache ;
505524}
506525
507- // === Added caches for advanced similarity metrics ===
526+ // Added caches for advanced similarity metrics
508527let docFreqCache = null ; // { term: documentFrequency }
509528let totalDocsCache = 0 ;
510529const licenseShingleCache = { } ; // { license_key: Set<string> }
0 commit comments