index.js
var __all_vconsole = document.querySelectorAll("script[data-type=vconsole]");
if (!__all_vconsole.length) {
var __script_vconsole = document.createElement("script");
__script_vconsole.type = "text/javascript";
__script_vconsole.setAttribute('data-type', 'vconsole')
__script_vconsole.src = "https://cdn.bootcdn.net/ajax/libs/vConsole/3.0.0/vconsole.min.js";
__script_vconsole.onload = function(e) {
var __all_newVConsole = document.querySelectorAll("script[data-type=newVConsole]");
if (!__all_newVConsole.length) {
var __script_newVConsole = document.createElement("script");
__script_newVConsole.type = "text/javascript";
__script_vconsole.setAttribute('data-type', 'newVConsole')
__script_newVConsole.text = "var vConsole = new VConsole();";
document.head.appendChild(__script_newVConsole);
}
}
document.head.appendChild(__script_vconsole);
}
var __bodyNode = document.querySelector('body');
var __config = {
attributes: false,
childList: true,
characterData: true,
subtree: true,
};
var __observer = new MutationObserver(function(mutationsList, observer) {
for (let mutation of mutationsList) {
console.log('====mutation:', mutation);
}
});
__observer.observe(__bodyNode, __config);
document.addEventListener('click', e => {
console.log('==========全局点击事件:', e);
})
webview.vue
<template>
<view>
<web-view :src="url"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
url: '',
title: '',
}
},
onLoad(option) {
let tUrl = option.url
delete option.url
this.title = option.__title__
delete option.__title__
let tParams = ''
for (const [key, value] of Object.entries(option)) {
tParams += `&${key}=${value}`
}
this.url = tUrl + tParams
},
onReady() {
var currentWebview = this.$scope.$getAppWebview();
setTimeout(() => {
let wv = currentWebview.children()[0]
wv.setStyle({
titleNView: {
titleText: this.title,
autoBackButton: true
}
})
wv.addEventListener('loading', function() {
plus.nativeUI.showWaiting()
}, false);
wv.addEventListener('loaded', () => {
wv.setJsFile('_www/static/injection/index.js')
plus.nativeUI.closeWaiting();
let title = wv.getTitle()
title = /(http|https):\/\/([\w.]+\/?)\S*/.test(title) ? '' : title
if (/(http|https):\/\/([\w.]+\/?)\S*/.test('http://' + title)) {
title = ''
}
if (!title.trim()) return
wv.setStyle({
titleNView: {
titleText: title,
}
})
}, false);
}, 200)
},
}
</script>
备注
- 拦截窗口url请求:overrideUrlLoading
- 设置预加载的文件:setJsFile(JS文件) ,setCssFile(CSS文件)