(function () {
    'use strict';
    if (window.HerContextManager) return;
    window.HerContextManager = {
        isValid() {
            try {
                return chrome.runtime && chrome.runtime.id && !chrome.runtime.lastError;
            } catch {
                return false;
            }
        },
        async safeStorageGet(keys, fallback = {}) {
            if (!this.isValid()) return fallback;
            return new Promise(resolve => {
                try {
                    chrome.storage.sync.get(keys, result => {
                        if (chrome.runtime.lastError) resolve(fallback);
                        else resolve(result);
                    });
                } catch {
                    resolve(fallback);
                }
            });
        },
        async safeStorageSet(obj) {
            if (!this.isValid()) return false;
            return new Promise(resolve => {
                try {
                    chrome.storage.sync.set(obj, () => {
                        resolve(!chrome.runtime.lastError);
                    });
                } catch {
                    resolve(false);
                }
            });
        }
    };
})(); 