UNPKG

3.21 kBJavaScriptView Raw
1import Light from "light";
2
3/**
4 * 打印二维码
5 */
6function printQRCode(hashpath) {
7 //93781 lighting工程不管是jsn项目还是纯h5项目都在控制台显示二维码
8 let path = location.pathname;
9 let env = __ENV__;
10
11 if (path === "/"){
12 path = "/index.html";
13 }
14 //H5永远打印!
15 print(path);
16 //plugins未配置时不报错
17 if(env.plugins && env.plugins.toString().indexOf("miniapp")>-1 && (!Light.onlyFor || Light.onlyFor === "miniapp")) {
18 if(path === "/index.html"){
19 print("/app.miniapp.js");
20 }else{
21 print(path.replace(/\.html/ig, ".miniapp.js"));
22 }
23 }
24 if(env.plugins && env.plugins.toString().indexOf("jsnative")>-1 && (!Light.onlyFor || Light.onlyFor === "jsn")){
25 if(path === "/index.html"){
26 print("/app.native.js");
27 }else{
28 print(path.replace(/\.html/ig, ".miniapp.js"));
29 }
30 }
31
32 //128463当无法联网的环境下时,不再去请求平台接口来显示二维码
33 function print(path) {
34 if(navigator.onLine){
35 let urlToScan = `http://${location.host}${path}${location.search}#${hashpath}`;
36 Light.ajax({
37 url:`https://fs-api.lightyy.com/service/utils/qrcode?url=${encodeURIComponent(urlToScan)}`,
38 type:"get",
39 dataType:"json",
40 success(data){
41 console.log("LightView 扫码 URL: " + urlToScan);
42 //91631 最新版本chrome浏览器的console无法打印二维码
43 console.log("%c ", "padding:75px 80px 75px;background:url(" + data.data + ") no-repeat;background-size:150px");
44 }
45 })
46 }
47 }
48}
49
50/**
51 * 计算节点的数量
52 * @param node
53 * @returns {number}
54 */
55function countNodes(node) {
56 let count = 1;
57 if(node.hasChildNodes()) {
58 let cnodes = node.childNodes;
59 for(let i=0; i<cnodes.length; i++) {
60 count = count + countNodes(cnodes.item(i))
61 }
62 }
63 return count;
64}
65/**
66 * 计算节点的层数
67 * @param node
68 * @returns {number}
69 */
70function countLevels(node) {
71 if(node.hasChildNodes()) {
72 let cnodes = node.childNodes;
73
74 let levels = [];
75 cnodes.forEach(function (n) {
76 levels.push( 1 + countLevels(n))
77 })
78 return Math.max(...levels)
79 }else{
80 return 1;
81 }
82}
83
84Light.filter("route", function (from, to, next) {
85 try {
86 next();
87 printQRCode(to.fullPath);
88
89 //93785 lighting工程在展示当前视图的dom节点数量和最深的节点层次
90 setTimeout(function () {
91 let count = countNodes(document.body);
92 let level = countLevels(document.body);
93 console.log(`当前视图的DOM节点总数为:${count},最深的DOM节点层次为:${level}`);//
94 },1000)
95 }catch (e) {
96 console.log(e)
97 }
98});
99
100//93779 lighting工程编译时如果是添加了-w选项则在控制台提示用户可以查看编译资源树及分析
101console.log(`%c查看编译资源详情请点击链接:http://${location.host}/stats.html`,"font-size:15px;color:red;font-weight:bolder");