当前位置:   article > 正文

谁说大厂都用 React,微信读书官网用 Vue

微信读书网页版用vue

思考一下,如何识别网站用的是 Vue 框架,还是 React 框架?本文以 Vue 为例,来看看如何检测任意网站使用的框架是 Vue。

面对这个问题时,起初可能没有太多的思路,我起初的想法是看看 js 中有没有 vue 相关的 api,或者 js 中有没有单独的 vue.js,也可以看看 css 是否使用了 scoped 特征的命名。

但是,这很不靠谱,并不通用,后来想到了以前写过一篇文章 Vue 创建的 app 实例最终去哪了,按照这个思路,如果使用了 Vue,那么一定会在 HTML 中某个 DOM 节点挂载 vue 的执行上下文,会在 DOM 中创建一个__vue_app__ 的属性。可能是以前是低版本,现在发现在 DOM 下创建的 __vue__属性,那么我们试一试微信读书的官网。

没错,确实有 __vue__属性,这里我用的选择器是 app,但是不同网站使用的不一定是 app,vue 实例会挂载到任意 DOM 上,为了更通用,我们可以遍历一下所有的节点:

  1. const all = document.querySelectorAll('*');
  2. let el
  3. for (let i = 0; i < all.length; i++) {
  4.   if (all[i].__vue__) {
  5.     el = all[i]
  6.     break
  7.   }
  8. }
  9. if (el) {
  10.   console.log('use vue', el);
  11.   var Vue = Object.getPrototypeOf(el.__vue__).constructor
  12.   while (Vue.super) {
  13.     Vue = Vue.super
  14.   }
  15.   console.log(Vue.version);
  16. }
  17. else {
  18.     console.log('not use vue');
  19. }

把这段脚本放到浏览器的 Console 中执行即可以看到使用的 Vue 版本:

一切进行的很顺利,直到我打开 vue 浏览器插件的时候,发现原来人家插件就可以检测到网站使用的是不是 Vue,我不是多此一举吗!

但是不能看到开发模式下类似下面的调试信息:

那我能不能把这个调试信息打开,可以偷窥下别人是如何使用 Vue 的,不然真的就白折腾了。

找到 vue-devtools 的源码,发现它检测是否使用 vue 代码如下(我上面那段代码其实是抄它的):

  1. const all = document.querySelectorAll('*')
  2. let el
  3. for (let i = 0; i < all.length; i++) {
  4.   if (all[i].__vue__) {
  5.     el = all[i]
  6.     break
  7.   }
  8. }
  9. if (el) {
  10.   var Vue = Object.getPrototypeOf(DOM.__vue__).constructor
  11.   while (Vue.super) {
  12.     Vue = Vue.super
  13.   }
  14.   win.postMessage({
  15.     devtoolsEnabled: Vue.config.devtools,
  16.     vueDetected: true
  17.   }, '*')
  18. }

经过一番查看后发现,通过下面这段代码控制是否显示调试 Vue 面板:

  1. chrome.devtools.inspectedWindow.eval(
  2.     '!!(window.__VUE_DEVTOOLS_GLOBAL_HOOK__.Vue || window.__VUE_DEVTOOLS_GLOBAL_HOOK__.apps.length)',
  3.     function (hasVue) {
  4.       if (!hasVue || created) {
  5.         return
  6.       }
  7.       clearInterval(checkVueInterval)
  8.       created = true
  9.       chrome.devtools.panels.create(
  10.         'Vue''icons/128.png''devtools.html',
  11.         panel => {
  12.           // panel loaded
  13.           panel.onShown.addListener(onPanelShown)
  14.           panel.onHidden.addListener(onPanelHidden)
  15.         }
  16.       )
  17.     }
  18. )

那我其实只要在 window 下添加 __VUE_DEVTOOLS_GLOBAL_HOOK__.Vue = Vue 即可让调试面板显示出来。

对我上面的脚本进行改造:

  1. const all = document.querySelectorAll('*');
  2. let el
  3. for (let i = 0; i < all.length; i++) {
  4.   if (all[i].__vue__) {
  5.     el = all[i]
  6.     break
  7.   }
  8. }
  9. if (el) {
  10.   console.log('use vue', el);
  11.   var Vue = Object.getPrototypeOf(el.__vue__).constructor
  12.   while (Vue.super) {
  13.     Vue = Vue.super
  14.   }
  15.   Vue.config.devtools = true;
  16.   window.__VUE_DEVTOOLS_GLOBAL_HOOK__.Vue = Vue;
  17.   console.log(Vue.version);
  18. }
  19. else {
  20.     console.log('not use vue');
  21. }

如果把这段代码放到浏览器的 Console 中执行,并不会起作用。我们需要放到浏览器插件 Tampermonkey 中执行, 它可自动在网站中注入一些脚本。脚本写法如下:

脚本准备好后,重启浏览器,效果如下:

再看看 B 站的:

到此,可以把使用网站的 vue 调试面板显示出来了。同理,检测网站是不是使用的是 React,也很简单,大家有兴趣试试。关于 Tampermonkey ,我会在后面的文章给大家介绍,非常牛逼。

大家加油!

长按关注

素燕《前端小课》

帮助 10W 人入门并进阶前端

官网:https://lefex.gitee.io/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/49550
推荐阅读
相关标签
  

闽ICP备14008679号