dha*_*jay 2 javascript gwt google-chrome-extension
我正在使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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
}
Run Code Online (Sandbox Code Playgroud)
这是我的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);
Run Code Online (Sandbox Code Playgroud)
加载浏览器时,默认的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)
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?代码中有什么问题。
Mus*_*NGA 10
如果您使用的是 chrome 清单版本 3,则必须使用它chrome.action(而不是)。chrome.browserAction例子 :
chrome.action.setBadgeBackgroundColor({ color: '#00FF00' })
chrome.action.setBadgeText({
tabId: activeTab.id,
text: 'text'
});
Run Code Online (Sandbox Code Playgroud)
使用
else {
chrome.browserAction.setBadgeText({text:localStorage.unreadCount});
}
Run Code Online (Sandbox Code Playgroud)
代替
else {
chrome.browserAction.setBadgeText(localStorage.unreadCount);
}
Run Code Online (Sandbox Code Playgroud)
browserAction.setBadgeText将 json 对象作为输入。
| 归档时间: |
|
| 查看次数: |
2679 次 |
| 最近记录: |