(TypeError):无法调用未定义的方法“setBadgeText”

问题描述 投票:0回答:3

我正在使用 GWT 实现 chrome 扩展。我在 manifest.json 中创建了映射并编写了外部 java 脚本文件。在 background.js 脚本中,我编写了函数,在该设置的徽章文本中,即 chrome.browserAction.setBadgeText( count);.我正在使用 gwt+jsni 方法从 gwt 类传递字符串值。gwt 代码中的方法是

     public static native void passValue(String updateCount) /*-{
        $wnd.realTimeUpdateCount(updateCount);
    }-*/;    
The Methods in the java script :  
        function realTimeUpdateCount(count) {

      localStorage.unreadCount =  count;
      updateCount();
        }

       function updateCount() {
         if (!localStorage.hasOwnProperty('unreadCount')) {
          chrome.browserAction.setIcon({path:"images/favIcon.ico"});
          chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
          chrome.browserAction.setBadgeText({text:"0"});
      } else {
           chrome.browserAction.setBadgeText(localStorage.unreadCount);
      }
       }

这是我的 manifest.json 文件:-

{
  "name": "A browser action with no icon that makes the page red",
  "version": "1.0",
  "background": { "scripts": ["background.js"] },
  "permissions": [
     "tabs",
    "webNavigation", 
    "http://127.0.0.1:8888/Engile.html?gwt.codesvr=127.0.0.1"
  ],
  "web_accessible_resources": [
    "4D1B5509A17D34BAE8DB33C353725838.cache.html"
  ],  
  "browser_action": {
    "name": "Make this page red",
    "default_icon": "images/favIcon.ico"

  },
  "manifest_version": 2
}

这是我的 background.js:-

function realTimeUpdateCount(count) {


    localStorage.unreadCount =  count;
    updateIcon();

}

if (chrome.runtime && chrome.runtime.onStartup) {
      chrome.runtime.onStartup.addListener(function() {
        console.log('Starting browser... updating icon.');
        updateIcon();
      });
    } else {

      chrome.windows.onCreated.addListener(function() {
        console.log('Window created... updating icon.');
        updateIcon();
      });
    }
function updateIcon() {



    if (!localStorage.hasOwnProperty('unreadCount')) {
        chrome.browserAction.setIcon({path:"images/favIcon.ico"});
        chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]});
        chrome.browserAction.setBadgeText({text:"?"});
      } else {

        chrome.browserAction.setBadgeText({
          text: localStorage.unreadCount
        });
      }

    }



function goToApp() {
    chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});
      console.log('Going to app...');
      chrome.tabs.getAllInWindow(undefined, function(tabs) {

        console.log('Opening the application ...');
        chrome.tabs.create({url: 'http://server.ensarm.com:8082/EngileLive/Engile.html'});
      });
    }
chrome.browserAction.onClicked.addListener(goToApp);

当浏览器加载时,默认值 0 正确显示在图标上。实时值在 updateCount() 方法中正确显示。但是控制来自这个 chrome.browserAction.setBadgeText(count);行,它抛出以下异常:

com.google.gwt.core.client.JavaScriptException: (TypeError): Cannot call method 'setBadgeText' of undefined
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
    at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
    at java.lang.Thread.run(Thread.java:662) 

有人可以帮忙解决这个问题吗?代码中有什么问题。

javascript gwt google-chrome-extension
3个回答
7
投票

您只能从后台脚本文件调用 chrome.browserAction 调用(我遇到了同样的问题)

参见:

Chrome 扩展:在运行时更改地址栏按钮背景颜色


5
投票

如果您使用的是 chrome 清单版本 3,则必须使用它的

chrome.action
(不是
chrome.browserAction
)。 例子:

chrome.action.setBadgeBackgroundColor({ color: '#00FF00' })
chrome.action.setBadgeText({
    tabId: activeTab.id,
    text: 'text'
});
            

2
投票

使用

else {
           chrome.browserAction.setBadgeText({text:localStorage.unreadCount});
      }

代替

else {
           chrome.browserAction.setBadgeText(localStorage.unreadCount);
      }

browserAction.setBadgeText
将 json 对象作为输入。

参考文献

© www.soinside.com 2019 - 2024. All rights reserved.