App.vue 3.91 KB
<template>
  <el-tabs>
    <el-tab-pane label="Debugger Apps">
      <div class="app-list">
        <template v-for="(app, key) in debuggerApps" :key="key">
          <div class="app-list-item">
            <div class="app-list-item__content">
              <el-input placeholder="子应用名称" v-model="app.name"> </el-input>
              <el-input placeholder="子应用地址" v-model="app.entry"></el-input>
            </div>
            <div class="app-list-item__btn">
              <el-button
                v-if="app.debugging"
                type=""
                @click="handleDebugging(key)"
              >
                <VideoPause style="width: 1em; height: 1em" />
              </el-button>
              <el-button v-else type="" @click="handleDebugging(key)">
                <VideoPlay style="width: 1em; height: 1em" />
              </el-button>
              <el-button type="" @click="deleteItem(key)">-</el-button>
              <el-button
                v-if="key === debuggerApps.length - 1"
                type=""
                @click="add"
                >+</el-button
              >
            </div>
          </div>
        </template>
      </div>
      <div class="btn">
        <el-button @click="start(true)"
          ><Refresh style="width: 1em; height: 1em" />
        </el-button>
        <el-button v-if="debuggerStatus" @click="end">
          <VideoPause style="width: 1em; height: 1em; margin-right: 8px" />
          结束调试
        </el-button>
        <el-button v-else @click="start"
          ><VideoPlay style="width: 1em; height: 1em; margin-right: 8px" />
          开启调试</el-button
        >
      </div>
    </el-tab-pane>
  </el-tabs>
</template>

<script setup>
import { reactive, ref, computed, watchEffect } from "vue";
const defaultApps = [{ name: "", entry: "", debugging: false }];

const { sessionStorage } =
  chrome.extension.getBackgroundPage().backgroundState || {};

const debuggerApps = ref(
  JSON.parse(
    window.localStorage.getItem("qx-debugger-apps") ||
      JSON.stringify(defaultApps)
  )
);

const debuggerStatus = ref(
  JSON.parse(window.localStorage.getItem("qx-debugger-status") || "false")
);

const filterDebuggerApps = computed(() =>
  debuggerApps.value.filter((app) => app.name && app.entry && app.debugging)
);

watchEffect(() => {
  window.localStorage.setItem(
    "qx-debugger-apps",
    JSON.stringify(debuggerApps.value || defaultApps)
  );

  chrome.runtime.sendMessage({
    type: "setSessionStorage",
    key: "QX_DEBUG_APPS",
    value: JSON.stringify(filterDebuggerApps.value),
  });
});

const DEVELOPER_MODE = ref(sessionStorage.DEVELOPER_MODE === "1");

function add() {
  debuggerApps.value.push(defaultApps[0]);
}

function deleteItem(index) {
  debuggerApps.value.splice(index, 1);

  if (index == 0 && !debuggerApps.length) {
    debuggerApps.value.push({ name: "", entry: "" });
  }
}

async function start() {
  chrome.runtime.sendMessage({ type: "reload" });
  debuggerStatus.value = true;
  window.localStorage.setItem("qx-debugger-status", "true");
  chrome.runtime.sendMessage({
    type: "setSessionStorage",
    key: "QX_DEBUG_APPS",
    value: JSON.stringify(filterDebuggerApps.value),
  });
}

async function end() {
  debuggerStatus.value = false;
  window.localStorage.setItem("qx-debugger-status", "false");

  chrome.runtime.sendMessage({
    type: "removeSessionStorage",
    key: "QX_DEBUG_APPS",
  });

  chrome.runtime.sendMessage({ type: "reload" });
}

function handleDebugging(index) {
  debuggerApps.value[index].debugging = !debuggerApps.value[index].debugging;
  chrome.runtime.sendMessage({ type: "reload" });
}
</script>

<style>
.app-list {
}
.app-list-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.app-list-item__content {
  width: 30%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-list-item__btn {
  margin-left: 10px;
}

.el-input {
  width: calc(50% - 5px);
}
</style>