diff --git a/src/main.js b/src/main.js index a95b8d5..7a9215a 100644 --- a/src/main.js +++ b/src/main.js @@ -4,7 +4,7 @@ // @author qianjiachun // @icon data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMjJweCIgaGVpZ2h0PSIyMHB4IiB2aWV3Qm94PSIwIDAgMjIgMjAiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8dGl0bGU+am95c291bmQvc2VsZWN0ZWQ8L3RpdGxlPgogICAgPGRlZnM+CiAgICAgICAgPGxpbmVhckdyYWRpZW50IHgxPSI1MCUiIHkxPSIwJSIgeDI9IjUwJSIgeTI9IjEwMCUiIGlkPSJsaW5lYXJHcmFkaWVudC0xIj4KICAgICAgICAgICAgPHN0b3Agc3RvcC1jb2xvcj0iI0YwQ0I5NSIgb2Zmc2V0PSIwJSI+PC9zdG9wPgogICAgICAgICAgICA8c3RvcCBzdG9wLWNvbG9yPSIjRTlCRTgwIiBvZmZzZXQ9IjEwMCUiPjwvc3RvcD4KICAgICAgICA8L2xpbmVhckdyYWRpZW50PgogICAgPC9kZWZzPgogICAgPGcgaWQ9ImpveXNvdW5kL3NlbGVjdGVkIiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSIgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIj4KICAgICAgICA8ZyBpZD0i57yW57uEIiB0cmFuc2Zvcm09InRyYW5zbGF0ZSgyLjc4NTc1MCwgMC43MTQyMjUpIiBmaWxsPSJ1cmwoI2xpbmVhckdyYWRpZW50LTEpIiBmaWxsLXJ1bGU9Im5vbnplcm8iPgogICAgICAgICAgICA8cGF0aCBkPSJNMTYuNDI4NiwwIEwxNi40Mjg2LDkuNjQzIEMxNi40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1LDEwLjc4NTc3NSBDOC44NDM4NTc2MywxMC43ODU3NzUgOS4zNTcsMTAuMjcyNjMyNiA5LjM1Nyw5LjY0MzAyNSBDOS4zNTcsOS4wMTMxOTQzMyA4Ljg0Mzg4NDU3LDguNTAwMDI1IDguMjE0MjUsOC41MDAwMjUgWiIgaWQ9IuW9oueKtiI+PC9wYXRoPgogICAgICAgIDwvZz4KICAgIDwvZz4KPC9zdmc+ // @namespace https://github.com/qianjiachun/joysound-tampermonkey -// @version 2022.02.11.03 +// @version 2022.02.12.01 // @description 🎵✨通用网页视频音效增强/优化。Enhance/Optimize video sound in web. // @match *://*/* // @include *://*/* @@ -19,12 +19,13 @@ // ==/UserScript== let Joysound = null; -let wasm = ""; + let joysoundList = []; +let hasLoadedJs = false; // 是否已加载js const LOCAL_NAME = "Ex_isJoysound"; // 是否开启joysound -const LOCAL_JS = "Ex_joysoundJs"; // 本地wasm代码 -const LOCAL_JS_VERSION = "Ex_joysoundJsVersion"; // 本地wasm版本号 -const JS_VERSION = "2022.02.11"; // wasm版本号,如有变动请修改这个值重新获取 +const LOCAL_JS = "Ex_joysoundJs"; // 本地js代码 +const LOCAL_JS_VERSION = "Ex_joysoundJsVersion"; // 本地js版本号 +const JS_VERSION = "2022.02.12"; // js版本号,如有变动请修改这个值重新获取 class CustomJoysound { constructor(node) { @@ -39,7 +40,25 @@ class CustomJoysound { } } -function enableJoysound() { +async function enableJoysound() { + // 开启joysound + // 判断有没有加载过js + if (!hasLoadedJs) { + // 先判断本地有没有js缓存,没有就下载,然后加载 + let js = GM_getValue(LOCAL_JS); + let jsVersion = GM_getValue(LOCAL_JS_VERSION); + if (js && jsVersion == JS_VERSION) { + eval(js); + } else { + let js = await getJs(); + GM_setValue(LOCAL_JS, js); + GM_setValue(LOCAL_JS_VERSION, JS_VERSION); + eval(js); + } + hasLoadedJs = true; + } + + // js确保已经加载 if (joysoundList.length > 0) { for (let i = 0; i < joysoundList.length; i++) { joysoundList[i].setEnabled(1); @@ -77,44 +96,48 @@ GM_registerMenuCommand("开启", enableJoysound); GM_registerMenuCommand("关闭", disableJoysound); function init() { - let ret = localStorage.getItem(LOCAL_NAME); - if (ret == 1) { - let count = 0; - let timer = setInterval(() => { - let videoNodes = document.querySelectorAll("video"); - if (videoNodes.length > 0) { - clearInterval(timer); - enableJoysound(); - } - if (count >= 240) { - clearInterval(timer); - } - count++; - }, 500); + if (!isEnabledJoysound()) { + return; } + let count = 0; + let timer = setInterval(() => { + if (hasVideoNode()) { + clearInterval(timer); + enableJoysound(); + } + if (count >= 240) { + clearInterval(timer); + } + count++; + }, 500); } -(async function () { - let js = GM_getValue(LOCAL_JS); - let jsVersion = GM_getValue(LOCAL_JS_VERSION); - if (js && jsVersion == JS_VERSION) { - eval(js); - init(); - } else { +function getJs() { + return new Promise(resolve => { GM_xmlhttpRequest({ method: "GET", url: "https://static.yximgs.com/udata/pkg/acfun-player/asyncPlayerPlugins.083319.js", responseType: "text", onload: function(response) { - wasm = response.response; + let ret = response.response; const STR = "b.safari"; - let index = wasm.indexOf(STR) + STR.length + 3; - let js = wasm.slice(0, index) + "Joysound=e;" + wasm.slice(index); - GM_setValue(LOCAL_JS, js); - GM_setValue(LOCAL_JS_VERSION, JS_VERSION); - eval(js); - init(); + let index = ret.indexOf(STR) + STR.length + 3; + let js = ret.slice(0, index) + "Joysound=e;" + ret.slice(index); + resolve(js); } }); - } -})() \ No newline at end of file + }) +} + +function hasVideoNode() { + let videoNodes = document.querySelectorAll("video"); + return videoNodes.length > 0; +} + +function isEnabledJoysound() { + return localStorage.getItem(LOCAL_NAME) == 1; +} + +(async function () { + init(); +})(); \ No newline at end of file