我正在尝试将 javascript 函数注入到运行服务器 url “www.myserver.com”的 web 视图中。但是,我无法这样做。我尝试了几种方法,但都没有成功。我尝试设置 js 警报,在读取 js 文件时不会弹出。我不确定我做错了什么。到目前为止,这是我的代码:我的 js 文件“page.js”:
(function() {
**alert("this is a test alert");**
if (window.page) {
return; // page has been loaded from somewhere, not to redefine it again
}
function Response(xhr) {
this.xhr = xhr;
this.status = xhr.status;
}
Response.prototype.getHeader = function(header) {
return this.xhr.getResponseHeader(header);
};
Response.prototype.getBody = function() {
return this.xhr.responseText;
};
function ajax(url, callback) {
//TODO support PUT method
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onerror = function(e) {
callback.onerror(e);
};
xhr.onreadystatechange = function(event) {
if (this.readyState != this.DONE || this.status == 0) {
return;
}
if (this.status != 200) {
var response = new Response(xhr);
callback.onfail(response);
return;
}
callback.onsuccess(this.responseText);
};
xhr.send();
};
var rpc = {
type: ["SMALL", "LARGE"],
exec: function(call, type) {// "call" conform to JSON-RPC 2.0
//TODO differentiate POST/GET based on rpc.type.SMALL/LARGE
var param = encodeURIComponent(JSON.stringify(call));
ajax("?_$b=" + param, {
onerror: function(e) {
//TODO use logging
alert("Error:"+e.toString());
},
onfail: function(response) {
//TODO use logging
alert("Failed:"+response.getBoby());
},
onsuccess: function(text) {
//TODO use logging
alert("page success:"+text);
},
});
}
};
var _$b = window.page = {
version: "0.1"
};
_$b.has = function(feature) {
// TODO support nested objects, e.g.: "cache.get"
return _$b[feature];
};
_$b.notify = function(id, data) {
// TODO invoke the function from native and send web event
// var event = document.createEvent('Event');
// event.initEvent(id, true, true);
// document.dispatchEvent(event);
alert("id:" + id + ";data:" + data);
};
_$b.navigate = function(url, title) {
rpc.exec(// JSON-RPC 2.0
{method: "navigate", params: {
"url": url,
"title": title
}}, rpc.type.SMALL);
};
_$b.cache = {};
_$b.cache.get = function(id) {
rpc.exec(
{method: "cache.get", params: {
"id": id,
}}, rpc.type.SMALL);
};
_$b.cache.put = function(id, obj, ts, ttl) {
rpc.exec(
{method: "cache.put", params: {
"id": id,
"obj": obj,
"ts": ts,
"ttl": ttl,
}}, rpc.type.LARGE);
};
_$b.cache.has = function(id) {
rpc.exec(
{method: "cache.has", params: {
"id": id,
}}, rpc.type.SMALL);
};
})();
这是我的网络视图代码:
@Override
public void onPageFinished(WebView view, String url) {
String javascript = "";
try{
AssetManager manager = view.getContext().getAssets();
InputStream is = manager.open("page.js");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while (( line = br.readLine()) != null) {
javascript += line;
}
is.close();
}
catch(Exception e){}
view.loadUrl("javascript:" + javascript);
}
webview.setWebChromeClient(new WebChromeClient() {});
webview.loadUrl(url);
任何想法如何让警报弹出?注意:当我删除 js 中的所有函数并仅保留警报时,警报会起作用,但是在我重新添加必要的 js 函数后,警报不再起作用。知道什么可能会阻止它按预期注入 js 吗?