4

我正在使用 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
}

这是我的背景.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 会在 Icon 上正确显示。实时值在 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) 

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

4

2 回答 2

7

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

看:

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

于 2013-01-25T04:46:27.473 回答
2

利用

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

代替

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

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

参考

于 2013-01-23T15:46:17.370 回答