App.vue 3.3 KB
<template>
  <el-tabs>
    <el-tab-pane label="Debugger Apps">
      <div class="app-list">
        <template v-for="(app, key) in QX_DEBUG_APPS" :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 type="" @click="deleteItem(key)">-</el-button>
              <el-button
                v-if="key === QX_DEBUG_APPS.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="DEVELOPER_MODE == 1 && filterDebuggerApps.length"
          @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 } from "vue";

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

const defaultApps = '[{"name":"","entry":""}]';

const sessionDebuggerApps = JSON.parse(
  sessionStorage.QX_DEBUG_APPS || defaultApps
);

const QX_DEBUG_APPS = reactive(
  JSON.parse(sessionDebuggerApps.length ? sessionDebuggerApps : defaultApps)
);

const filterDebuggerApps = computed(() =>
  QX_DEBUG_APPS.filter((item) => item.name && item.entry)
);

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

console.log("sessionStorage", sessionStorage);

function add() {
  QX_DEBUG_APPS.push({ name: "", entry: "" });
}

function deleteItem(index) {
  QX_DEBUG_APPS.splice(index, 1);

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

function validate(refresh) {
  if (filterDebuggerApps.value.length || refresh) {
    return true;
  }

  return false;
}

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

async function start(refresh) {
  if (validate(refresh)) {
    DEVELOPER_MODE.value = "1";
    save();
    chrome.runtime.sendMessage({
      type: "setSessionStorage",
      key: "DEVELOPER_MODE",
      value: "1",
    });

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

async function end() {
  DEVELOPER_MODE.value = null;

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

  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>