我正在使用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值在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)
有人可以帮忙解决这个问题吗?代码中有什么问题。
答案 0 :(得分:4)
您只能从后台脚本文件中调用chrome.browserAction调用(我遇到了同样的问题)
请参阅:
Chrome Extension: Change Address bar Button Background color at runtime