【问题标题】:Chrome Extension "Uncaught SyntaxError: Unexpected identifier" on `storageArea.set``storageArea.set` 上的 Chrome 扩展“Uncaught SyntaxError: Unexpected identifier”
【发布时间】:2026-01-04 22:10:01
【问题描述】:
var getShortenedUrl = function () {

  chrome.tabs.getSelected(null, function (tab) {

    var request_data = {
      'command': 'generate',
      'params': {
        'url': tab.url,
        'code': text_field.value
      }
    }

    chrome.extension.sendRequest(request_data, function (data) {
      switch (data.status) {
        case 'OK':
          setTextField(data.shortened_url)
          bindBtnToCoopy()
          chrome.storage.local.get(data.shortened_url, function (arr) {
            if (!arr[data.shortened_url]) {
              chrome.storage.local.set(
                {data.shortened_url:
                 tab.url}) /* <-- this thing throws an error */
            }
          })
          break
          /* ... */
      }
    })
  })
}

https://github.com/noformnocontent/git-io-chrome/blob/master/chrome/popup.js#L96


如果我将chrome.storage.local.set 部分注释掉,一切都是“完美的”

【问题讨论】:

标签: javascript google-chrome-extension


【解决方案1】:

正如question@pimvdb 中提到的,要保存一个对象,我必须使用一个对象。

if (!arr[data.shortened_url]) {
  var urlPair = {}
  urlPair[data.shortened_url] = tab.url
  chrome.storage.local.set(urlPair)
}

从那时起我也发布了“Git.io for Chrome”的v.0.5,更多信息请参见http://git.io/crome

【讨论】: