我正在尝试将javascript函数注入到我的webview中,运行服务器URL" www.myserver.com"。但是,我无法这样做。我尝试了几种方法但没有成功。我尝试设置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吗?
答案 0 :(得分:0)
考虑这段代码。这是简单的html&在page.js之后建模的javascript代码
<html>
<script type="text/javascript">
(function() { alert('inside function'); })
alert('ok');
</script>
</html>
执行此操作,您可以推导出函数内部的警报不会在开始时执行,除非您专门调用它。
点击即可运行您的功能:(试一下)
<html>
<input type="button" onclick="run()" value="Start" />
<script type="text/javascript">
var run = (function() { alert('inside function'); })
alert('ok');
</script>
</html>
运行您的功能[自行运行](试试这个)
<html>
<script type="text/javascript">
var run = (function() { alert('inside function'); })
alert('ok');
//you need to execute your function that you have named
run();
</script>
</html>