测试代码
// ==UserScript==
// @name 打开音乐文件夹测试
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description try to take over the world!
// @author You
// @match https://cloud.189.cn/web/main/file/folder/71461113680969543
// @require https://scriptcat.org/lib/513/2.0.0/ElementGetter.js
// @run-at document-start
// @grant unsafeWindow
// ==/UserScript==
(function () {
'use strict';
/*
// 这个成功 {isTrusted: true}
elmGetter.get('div').then(div => {
console.log('节点必须有', div);
loadScript('https://code.jquery.com/jquery-3.6.4.min.js').then(success => {
console.log('success', success);
console.log('jQuery:', unsafeWindow?.jQuery?.fn?.jquery || 'loadScript 失败了');
}, error => {
console.log('error', error);
});
});
*/
// 这个失败 {isTrusted: false},script未写入
elmGetter.get('.p-web-audioplayer').then(div => {
console.log('节点必须有', div);
loadScript('https://code.jquery.com/jquery-3.6.4.min.js').then(success => {
console.log('success', success);
console.log('jQuery:', unsafeWindow?.jQuery?.fn?.jquery || 'loadScript 失败了');
}, error => {
console.log('error', error);
});
});
function loadScript(src) {
if (!window.instances) {
window.instances = {};
}
if (!window.instances[src]) {
window.instances[src] = new Promise((resolve, reject) => {
const script = document.createElement("script")
script.src = src;
script.type = "text/javascript";
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
return window.instances[src];
};
// Your code here...
})();
感觉上在 new MutationObserver 之前或者使用 MutationObserver 后尽可能快的写入script标签是成功的
反之无法写入(document.head里没有),直观表现就是返回状态仍是success ,但 isTrusted: false
手写 MutationObserver 触发代码似乎表现也一样的
搞不懂喽,请老师看一下属于什么事故这是
猫和猴子表现也不一样,猫直接没找到节点 @王一之