Chrome扩展Storage API语法报错:第二行出现'Uncaught SyntaxError: Unexpected token )'?
Let's break down both of the errors you're hitting with the Chrome Extension Storage API, step by step:
1. Uncaught SyntaxError: Unexpected token )
This is a basic JavaScript syntax error, plain and simple. The root cause is that your code has a mismatched or misplaced parenthesis in the line where you're calling the Storage API's get method.
Common scenarios that trigger this:
- You accidentally added an extra closing parenthesis when defining the
getcall, like this:chrome.storage.local.get('myStoredKey', ); // Extra ) here breaks syntax - You messed up the bracket/parenthesis pairing for your callback function, such as forgetting an opening brace or closing parenthesis:
chrome.storage.local.get('myStoredKey', function(result) { console.log(result) ) // Missing closing }
The JavaScript parser can't make sense of the invalid syntax, so it throws this error immediately. Double-check the second line of your code to fix all parenthesis/brace mismatches.
2. extensions::StorageArea:35 Uncaught Error: Invocation of form get(string) doesn't match definition...
This error comes from not following the Chrome Storage API's required method signature.
Looking at the error message's definition: get(optional string or array or object keys, function callback), the second parameter (the callback function) is required when using the traditional callback-based version of the API. You can't just pass a key and omit the callback—this violates the API's expected input structure.
Here's how to fix it:
Option 1: Use the required callback (works for all Chrome versions)
chrome.storage.local.get('myStoredKey', function(storageResult) { // Do something with the result, like log it or update your UI console.log('Fetched value:', storageResult.myStoredKey); });
Option 2: Use Promise-based syntax (Chrome 88+)
If you're targeting Chrome 88 or newer, you can skip the callback entirely by using the Promise version of the API (no more mandatory callback):
async function fetchStoredValue() { const storageResult = await chrome.storage.local.get('myStoredKey'); console.log('Fetched value:', storageResult.myStoredKey); } fetchStoredValue();
Just make sure your extension's manifest or target Chrome version supports this syntax.
内容的提问来源于stack exchange,提问作者difoxy2

