forked from phith0n/vueinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (83 loc) · 2.36 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
function findVueRoot(root) {
const queue = [root];
while (queue.length > 0) {
const currentNode = queue.shift();
if (currentNode.__vue__ || currentNode.__vue_app__ || currentNode._vnode) {
console.log("vue detected on root element:", currentNode);
return currentNode
}
for (let i = 0; i < currentNode.childNodes.length; i++) {
queue.push(currentNode.childNodes[i]);
}
}
return null;
}
function findVueRouter(vueRoot) {
let router;
try {
if (vueRoot.__vue_app__) {
router = vueRoot.__vue_app__.config.globalProperties.$router.options.routes
console.log("find router in Vue object", vueRoot.__vue_app__)
} else if (vueRoot.__vue__) {
router = vueRoot.__vue__.$root.$options.router.options.routes
console.log("find router in Vue object", vueRoot.__vue__)
}
} catch (e) {}
try {
if (vueRoot.__vue__ && !router) {
router = vueRoot.__vue__._router.options.routes
console.log("find router in Vue object", vueRoot.__vue__)
}
} catch (e) {}
return router
}
function walkRouter(rootNode, callback) {
const stack = [{node: rootNode, path: ''}];
while (stack.length) {
const { node, path} = stack.pop();
if (node && typeof node === 'object') {
if (Array.isArray(node)) {
for (const key in node) {
stack.push({node: node[key], path: mergePath(path, node[key].path)})
}
} else if (node.hasOwnProperty("children")) {
stack.push({node: node.children, path: path});
}
}
callback(path, node);
}
}
function mergePath(parent, path) {
if (path.indexOf(parent) === 0) {
return path
}
return (parent ? parent + '/' : '') + path
}
function main() {
const vueRoot = findVueRoot(document.body);
if (!vueRoot) {
console.error("This website is not developed by Vue")
return
}
let vueVersion;
if (vueRoot.__vue__) {
vueVersion = vueRoot.__vue__.$options._base.version;
} else {
vueVersion = vueRoot.__vue_app__.version;
}
console.log("Vue version is ", vueVersion)
const routers = [];
const vueRouter = findVueRouter(vueRoot)
if (!vueRouter) {
console.error("No Vue-Router detected")
return
}
console.log(vueRouter)
walkRouter(vueRouter, function (path, node) {
if (node.path) {
routers.push({name: node.name, path})
}
})
return routers
}
console.table(main())