Selaa lähdekoodia

修复部分版本浏览器不支持BroadcastChannel,替换使用postMessage。

zss 2 vuotta sitten
vanhempi
sitoutus
f0df8535e2
1 muutettua tiedostoa jossa 18 lisäystä ja 5 poistoa
  1. 18 5
      content/main.js

+ 18 - 5
content/main.js

@@ -7,7 +7,8 @@ class Home {
 		this.itemQuery = { ImageTypes: "Backdrop", EnableImageTypes: "Logo,Backdrop", IncludeItemTypes: "Movie,Series", SortBy: "ProductionYear, PremiereDate, SortName", Recursive: true, ImageTypeLimit: 1, Limit: 10, Fields: "ProductionYear", SortOrder: "Descending", EnableUserData: false, EnableTotalRecordCount: false };
 		this.coverOptions = { type: "Backdrop", maxWidth: 3000 };
 		this.logoOptions = { type: "Logo", maxWidth: 3000 };
-
+        if (!'BroadcastChannel' in window && !'postMessage' in window)
+            return;
 		setInterval(() => {
 			if (window.location.href.indexOf("!/home") != -1) {
 				if ($(".view:not(.hide) .misty-banner").length == 0 && $(".misty-loading").length == 0) {
@@ -48,14 +49,26 @@ class Home {
 	static injectCode(code) {
 		let hash = md5(code + Math.random().toString());
 		return new Promise((resolve, reject) => {
-			const channel = new BroadcastChannel(hash);
-			channel.addEventListener("message", (event) => resolve(event.data));
+			if ('BroadcastChannel' in window) {
+				const channel = new BroadcastChannel(hash);
+				channel.addEventListener("message", (event) => resolve(event.data));
+			} else if ('postMessage' in window) {
+				window.addEventListener("message", (event) => {
+					if (event.data.channel === hash) {
+						resolve(event.data.message);
+					}
+				});
+			}
 			const script = `
 			<script class="I${hash}">
 				setTimeout(async ()=> {
 					async function R${hash}(){${code}};
-					const channel = new BroadcastChannel("${hash}");
-					channel.postMessage(await R${hash}());
+					if ("BroadcastChannel" in window) {
+						  const channel = new BroadcastChannel("${hash}");
+						  channel.postMessage(await R${hash}());
+				    } else if ('postMessage' in window) {
+						  window.parent.postMessage({channel:"${hash}",message:await R${hash}()}, "*");
+					}
 					document.querySelector("script.I${hash}").remove()
 				}, 16)
 			</script>