Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension storage.sync.set in content script throws exception

I have a weird issue that wasted too much time. I have content script that tries to read some data from the storage.sync API but it fails.

I've added the storage permission and I was able to work with the storage.sync API in the options.js file.

Can you spot what went wrong?

document.addEventListener('DOMContentLoaded', function () {   

     chrome.storage.sync.get([
            'var1',
            'var2',        
        ], function(items) {        
            alert("got it");
     });
}

This is the error message:

CONTENT_SCRIPT context for xxxxx) Lazy require of extension.binding did not set the binding field

(CONTENT_SCRIPT context for xxxxx) extensions::lastError:82: Uncaught TypeError: Cannot convert undefined or null to object{TypeError: Cannot convert undefined or null to object at Object.clear (extensions::lastError:82:23) at handleResponse (extensions::sendRequest:84:15) at restore at HTMLDocument.}

like image 451
embedded Avatar asked Dec 17 '16 09:12

embedded


People also ask

Can content scripts access Chrome storage?

User data can be automatically synced with Chrome sync (using storage. sync ). Your extension's content scripts can directly access user data without the need for a background page.

Can Chrome extensions use localStorage?

One way to share data between a background script and the other scripts that make up the extension is to save the data to a location which is accessible to all the scripts in the extension. We can use the browser localStorage API or chrome. storage which is specific to Chrome extensions.

Does Chrome sync local storage?

sync , the stored data will automatically be synced to any Chrome browser that the user is logged into, provided the user has sync enabled. When Chrome is offline, Chrome stores the data locally. The next time the browser is online, Chrome syncs the data. Even if a user disables syncing, storage.


3 Answers

I had the same error and it was caused due to debugger stopping the flow of execution in the middle. Once I removed the debugger / breakpoints in my 'Content Script' and 'Background Script' and reloaded everything then I didn't see the error.

like image 59
Sudheer Avatar answered Oct 28 '22 20:10

Sudheer


I was also getting this same error. I fixed it by disabling and re-enabling my Chrome browser extensions. Hope that helps.

like image 32
crgolden Avatar answered Oct 28 '22 18:10

crgolden


I had the same problem when injecting code into a html page. Using new id's immediately after injecting them into the page html seems to crash the debugger but not the page itself. Everything works and the error message only pops once on injecting the code. It looks like it was coming from the cache. It just happened once on refreshing the page and never after clearing cache... Sorry not very useful, but this is what I had and could not reproduce anymore.

like image 24
user7325014 Avatar answered Oct 28 '22 19:10

user7325014