Nolovenodie丶 3 лет назад
Родитель
Сommit
283c5438b3
1 измененных файлов с 39 добавлено и 11 удалено
  1. 39 11
      content/main.js

+ 39 - 11
content/main.js

@@ -15,24 +15,21 @@ class Home {
 		}
 		CommonUtils.selectWait(".section0 .backdropCard", async () => {
 			await this.initBanner();
+			this.initEvent();
 		});
 	}
 
-	static injectCall(func, arg) {
-		let hash = md5(arg);
+	static injectCode(code) {
+		let hash = md5(code);
 		return new Promise((resolve, reject) => {
 			const channel = new BroadcastChannel(hash);
 			channel.addEventListener("message", (event) => resolve(event.data));
 			const script = `
 			<script class="I${hash}">
 				setTimeout(async ()=> {
-					var client = await new Promise((resolve, reject) => {
-						setInterval(() => {
-							if (window.ApiClient != undefined) resolve(window.ApiClient);
-						}, 16);
-					});
+					async function R${hash}(){${code}};
 					const channel = new BroadcastChannel("${hash}");
-					channel.postMessage(await client.${func}(${arg}));
+					channel.postMessage(await R${hash}());
 					document.querySelector("script.I${hash}").remove()
 				}, 16)
 			</script>
@@ -41,6 +38,19 @@ class Home {
 		});
 	}
 
+	static injectCall(func, arg) {
+		const script = `
+		// const client = (await window.require(["ApiClient"]))[0];
+		const client = await new Promise((resolve, reject) => {
+			setInterval(() => {
+				if (window.ApiClient != undefined) resolve(window.ApiClient);
+			}, 16);
+		});
+		return await client.${func}(${arg})
+		`;
+		return this.injectCode(script);
+	}
+
 	static getItems(query) {
 		return this.injectCall("getItems", "client.getCurrentUserId(), " + JSON.stringify(query));
 	}
@@ -69,7 +79,7 @@ class Home {
 
 		// 插入数据
 		const data = await this.getItems(this.itemQuery);
-		console.log(data);
+		// console.log(data);
 		data.Items.forEach(async (item) => {
 			const detail = await this.getItem(item.Id),
 				itemHtml = `
@@ -78,7 +88,7 @@ class Home {
 				<div class="misty-banner-info padded-left padded-right">
 					<h1>${detail.Name}</h1>
 					<div><p>${detail.Overview}</p></div>
-					<div><button>MORE</button></div>
+					<div><button onclick="appRouter.showItem('${detail.Id}')">MORE</button></div>
 				</div>
 			</div>
 			`,
@@ -89,7 +99,7 @@ class Home {
 				$(".misty-banner-body").append(itemHtml);
 				$(".misty-banner-logos").append(logoHtml);
 			}
-			console.log(item.Id, detail);
+			// console.log(item.Id, detail);
 		});
 
 		let complete = 0;
@@ -132,6 +142,24 @@ class Home {
 			}
 		}, 16);
 	}
+
+	/* 初始事件 */
+	static initEvent() {
+		// 通过注入方式, 方可调用appRouter函数, 以解决Content-Script window对象不同步问题
+		const script = `
+		// 挂载appRouter
+		if (!window.appRouter) window.appRouter = (await window.require(["appRouter"]))[0];
+		// 重新挂载媒体库事件
+		const librarys = document.querySelectorAll(".view:not(.hide) .section0 .backdropCard");
+		librarys.forEach(library => {
+			library.addEventListener("mousedown", () => {
+				const dataId = library.getAttribute("data-id");
+				appRouter.showItem(dataId)
+			});
+		});
+		`;
+		this.injectCode(script);
+	}
 }
 
 // 运行