前言
在iOS原生開發(fā)之余也會寫一些網(wǎng)頁锅减,尤其在最近一個項目中使用JavaScriptBridge與網(wǎng)頁交互中涉及比較多只搁,其中一個場景時由于網(wǎng)頁需要調(diào)用原生的功能盗痒,導致在開發(fā)中如果只在Chrome里開發(fā)調(diào)試不在APP里面調(diào)試就沒法進行钩杰,導致開發(fā)效率下降很多,為此,我特地研究寫了一個JSBridge橋接工具庫苛萎,讓網(wǎng)頁在APP里時調(diào)用APP的方法桨昙,在APP之外的瀏覽器里使用JSBridge對應的方法,這樣就可以無縫在APP內(nèi)和APP外開發(fā)調(diào)試了腌歉,以后時機合適后會把所有內(nèi)容都公布出來蛙酪,其中整理的知識點如下:
1、JS實現(xiàn)粘貼內(nèi)容到粘貼板
// 實現(xiàn)原理究履,創(chuàng)建一個看不見的Textarea滤否,復制里面的內(nèi)容
bridge.handlers["copyText"] = function(data,callback){
if(data == null || data["text"] == null){
return;
}
var txt = data["text"];
var copyDiv = document.createElement('div');
copyDiv.innerHTML = "<textarea id='copyJsTextToBoardContents' style='height:0px;'>" +
data["text"] +"</textarea>";
document.documentElement.appendChild(copyDiv);
var e=document.getElementById("copyJsTextToBoardContents");//對象是contents
e.select(); //選擇對象
try{
document.execCommand("Copy"); //執(zhí)行瀏覽器復制命令
setTimeout(function() {
document.documentElement.removeChild(copyDiv) }, 1);
if(callback){
callback("復制成功");
}
}
}catch(e){
if(callback){callback("復制失敗");}
}
}
2脸狸、展示想Android那樣的Toast
bridge.handlers["showToast"] = function(data,callback){
if(data == null || data["text"] == null){
data = {"text":" "};
}
var toastDiv=document.getElementById("toastDivId");
if(toastDiv != null){
document.documentElement.removeChild(toastDiv);
}
toastDiv = document.createElement('div');
toastDiv.id = "toastDivId";
//設置div的屬性
toastDiv.innerHTML = "<div style='background:#000;border-radius:5px;padding:5px;font-size:10px;color:#Fff;filter:alpha(Opacity=60);-moz-opacity:0.6;opacity: 0.6'>" +
data["text"] +
"</div>"; //設置顯示的數(shù)據(jù)最仑,可以是標簽.
toastDiv.style.cssText="min-width:10px;max-width:200px;margin:0 auto;background:white;position:absolute;left:40%;top:50%;";
document.documentElement.appendChild(toastDiv);
setTimeout(function() {
var toastDiv2=document.getElementById("toastDivId");
if(toastDiv2 != null){
document.documentElement.removeChild(toastDiv2);
}
}, 1500);
};