webview-screenshot-comparison.uvue 2.4 KB
Newer Older
1
<template>
2 3
  <web-view id="webview-screenshot" class="webview-screenshot" :webview-styles='webviewStyles' :src="src"
    @loaded="loaded" @error="error">
4 5 6 7
  </web-view>
</template>

<script>
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9
  import { state } from '@/store/index.uts'

10 11 12 13 14 15 16
  export default {
    data() {
      return {
        baseSrc: '',
        src: '',
        webviewContext: null as WebviewContext | null,
        isLoaded: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18
        headerHeight: state.headerHeight,
        devicePixelRatio: state.devicePixelRatio,
19 20 21
        webviewStyles: {
          progress: false
        }
22 23 24 25 26 27 28
      }
    },
    onReady() {
      this.webviewContext = uni.createWebviewContext('webview-screenshot', this)
    },
    methods: {
      appendWebHeadPlaceholder() {
雪洛's avatar
雪洛 已提交
29
        if (this.src.indexOf('pages/template/navbar-lite/navbar-lite') > -1) {
DCloud-WZF's avatar
DCloud-WZF 已提交
30 31 32 33
          this.webviewContext?.evalJS(`
            const uniNavbar = document.querySelector('.uni-navbar');
            uniNavbar.style.paddingTop = '${state.headerHeight - 44}px';
          `)
雪洛's avatar
雪洛 已提交
34
        } else if (this.src.indexOf('pages/template/scroll-fold-nav/scroll-fold-nav') > -1) {
DCloud-WZF's avatar
DCloud-WZF 已提交
35 36 37 38 39 40 41 42 43
          this.webviewContext?.evalJS(`
            const heightSeat = document.querySelector('.height-seat');
            heightSeat.style.height = '125px';
            heightSeat.style.backgroundColor = '#f0f8ff';
            const topBox = document.querySelector('.top-box');
            topBox.style.top = '35px';
          `)
        } else {
          this.webviewContext?.evalJS(`
44 45 46 47 48
  const hasWebHeadPlaceholder = document.querySelector('.web-head-placeholder-for-screenshot-comparison');
  if (hasWebHeadPlaceholder) {
    return;
  }
  const webHeadPlaceholder = document.createElement('div');
DCloud-WZF's avatar
DCloud-WZF 已提交
49
  webHeadPlaceholder.style.height = '${state.headerHeight - 44}px';
50 51 52 53 54 55
  webHeadPlaceholder.style.backgroundColor = '#007aff';
  webHeadPlaceholder.classList.add('web-head-placeholder-for-screenshot-comparison');
  const uniPage = document.querySelector('uni-page');
  const uniPageHead = document.querySelector('uni-page-head');
  uniPage.insertBefore(webHeadPlaceholder, uniPageHead);
  `);
DCloud-WZF's avatar
DCloud-WZF 已提交
56
        }
57 58 59
      },
      loaded() {
        this.isLoaded = true
DCloud-WZF's avatar
DCloud-WZF 已提交
60
        this.appendWebHeadPlaceholder();
61 62 63 64 65 66 67 68 69 70 71 72
      },
      error(event : WebViewErrorEvent) {
        console.log('webview load error', JSON.stringify(event.detail));
      }
    }
  }
</script>
<style>
  .webview-screenshot {
    flex: 1;
  }
</style>