Skip to content

Commit

Permalink
fix: 🐞 使用unsafeWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocyss committed Apr 12, 2024
1 parent c078a8c commit 040f91b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import storeVue from "@/components/conf/store.vue";
import userVue from "@/components/conf/user.vue";
import logVue from "@/components/conf/log.vue";
import { onMounted, ref } from "vue";
import { useStore } from "./hooks/useStore";
logger.info("BoosHelper挂载成功");
ElMessage("BoosHelper挂载成功!");
const { storeInit } = useStore();
const confBox = ref(false);
const confs = {
store: { name: "存储配置", component: storeVue },
Expand All @@ -42,8 +44,9 @@ function themeChange() {
document.documentElement.classList.toggle("dark", dark.value);
GM_setValue("theme-dark", dark.value);
}
onMounted(() => {
onMounted(async () => {
document.documentElement.classList.toggle("dark", dark.value);
await storeInit();
});
</script>

Expand Down
6 changes: 4 additions & 2 deletions src/hooks/useApplying/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GreetError, PublishError } from "@/types/deliverError";
import axios from "axios";
import { useStore } from "../useStore";
import { ElMessage } from "element-plus";
import { unsafeWindow } from "$";

const { userInfo } = useStore();
export function requestCard(params: { securityId: string; lid: string }) {
Expand Down Expand Up @@ -36,7 +37,7 @@ export async function sendPublishReq(
jobId: data.encryptJobId,
lid: data.lid,
};
const token = userInfo.value?.token || window?._PAGE?.zp_token;
const token = unsafeWindow?._PAGE?.zp_token || window?._PAGE?.zp_token;
if (!token) {
ElMessage.error("没有获取到token,请刷新重试");
throw new PublishError("没有获取到token");
Expand Down Expand Up @@ -77,7 +78,8 @@ export async function requestBossData(
throw new GreetError(errorMsg || "重试多次失败");
}
const url = "https://www.zhipin.com/wapi/zpchat/geek/getBossData";
const token = userInfo.value?.token || window?._PAGE?.zp_token;
// userInfo.value?.token 不相等!
const token = unsafeWindow?._PAGE?.zp_token || window?._PAGE?.zp_token;
if (!token) {
ElMessage.error("没有获取到token,请刷新重试");
throw new GreetError("没有获取到token");
Expand Down
14 changes: 12 additions & 2 deletions src/hooks/useApplying/handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import {
GreetError,
} from "@/types/deliverError";
import { useStore } from "../useStore";
import { unsafeWindow } from "$";

const { modelData, requestGpt } = useModel();
const { formData } = useConfFormData();
const { todayData } = useStatistics();
const { userInfo } = useStore();

export const communicated: handleCFn = (h) => {
// h.push(async ({ data }) => {
// try {
Expand Down Expand Up @@ -203,7 +205,11 @@ export const activityFilter: handleCFn = (h) =>
export const customGreeting: handleCFn = (h) => {
const template = miTem.compile(formData.customGreeting.value);
const uid =
userInfo.value?.userId || window?._PAGE?.uid || window?._PAGE?.userId;
userInfo.value?.userId ||
unsafeWindow?._PAGE?.uid ||
unsafeWindow?._PAGE?.userId ||
window?._PAGE?.uid ||
window?._PAGE?.userId;
if (!uid) {
ElMessage.error("没有获取到uid,请刷新重试");
throw new GreetError("没有获取到uid");
Expand Down Expand Up @@ -234,7 +240,11 @@ export const aiGreeting: handleCFn = (h) => {
(v) => v.key === formData.aiGreeting.model
);
const uid =
userInfo.value?.userId || window?._PAGE?.uid || window?._PAGE?.userId;
userInfo.value?.userId ||
unsafeWindow?._PAGE?.uid ||
unsafeWindow?._PAGE?.userId ||
window?._PAGE?.uid ||
window?._PAGE?.userId;
if (!uid) {
ElMessage.error("没有获取到uid,请刷新重试");
throw new GreetError("没有获取到uid");
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref } from "vue";
import { getRootVue, useHookVueData } from "./useVue";
import { logger } from "@/utils/logger";

const userInfo = ref<{
userId: number;
Expand Down Expand Up @@ -31,6 +32,7 @@ const storeInit = async () => {
const v = await getRootVue();
const store = v?.$store?.state;
userInfo.value = store?.userInfo;
logger.debug("userInfo: ", userInfo.value);
};

export const useStore = () => {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useWebSocket/protobuf.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import protobuf from "protobufjs";
import { TechwolfChatProtocol } from "./type";
import { unsafeWindow } from "$";

var Root = protobuf.Root,
Type = protobuf.Type,
Expand Down Expand Up @@ -92,6 +93,6 @@ export class Message {
return this.msg.buffer.slice(0, this.msg.byteLength);
}
send() {
window.ChatWebsocket.send(this);
(window.ChatWebsocket || unsafeWindow.ChatWebsocket).send(this);
}
}
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ export class Logger {

export const logger = new Logger(
process.env.NODE_ENV === "production"
? undefined
? { level: LogLevel.DEBUG, print: true }
: { level: LogLevel.DEBUG, print: true }
);
3 changes: 2 additions & 1 deletion update.log
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
v0.1.4 🌟🤡 卡片状态适配白天,修复将gpt提示发送给boos
v0.1.4 🌟🤡 卡片状态适配白天,修复将gpt提示发送给boos
v0.1.5 🤡 修复编译后拿不到window数据导致报错的严重Bug
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default defineConfig(() => {
homepage: "https://github.com/Ocyss/boos-helper",
match: ["https://*.zhipin.com/*"],
author: "Ocyss",

grant: ["unsafeWindow"],
// "run-at": "document-start",
},
build: {
Expand Down

0 comments on commit 040f91b

Please sign in to comment.