油猴脚本:优化控制台日志打印。

pull/291/head
王良 2024-04-10 13:00:24 +08:00
parent 80e8024faf
commit 6e6be5e5a1
3 changed files with 19 additions and 16 deletions

View File

@ -472,5 +472,4 @@
window.dispatchEvent(new Event('urlchange'))
});
}
console.log('ds_github loaded')
})();

View File

@ -6,4 +6,4 @@ window.__ds_global__={
GM_setValue: () => {},
GM_notification: () => {}
}
console.log('ds_global loaded')
console.log("ds_global completed")

View File

@ -3,20 +3,24 @@ const path = require('path')
const log = require('../../utils/util.log')
let scripts
function buildScript (sc, content) {
const grant = sc.grant
const pre = 'window.addEventListener("load",' +
' ()=> { \r\n'
let grantSc = ''
for (const item of grant) {
grantSc += (item.indexOf('.') > 0 ? '' : 'const ') + item + ' = window.__ds_global__[\'' + item + '\']\r\n'
function buildScript (sc, content, scriptName) {
let grantStr = ''
for (const item of sc.grant) {
if (grantStr.length > 0) {
grantStr += '\r\n'
}
grantStr += (item.indexOf('.') > 0 ? '' : 'const ') + item + ' = window.__ds_global__[\'' + item + '\']'
}
const tail = ';' + content + '\r\n' +
'})'
return pre + grantSc + tail
return 'window.addEventListener("load", ()=> {\r\n' +
grantStr + ';\r\n' +
content +
(scriptName ? `\r\nconsole.log("ds_${scriptName} completed")` : '') +
'\r\n})' +
(scriptName ? `\r\nconsole.log("ds_${scriptName} loaded")` : '')
}
function loadScript (content) {
function loadScript (content, scriptName) {
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_openInTab
@ -51,7 +55,7 @@ function loadScript (content) {
}
const script = arr[start + 1].trim()
sc.script = buildScript(sc, script)
sc.script = buildScript(sc, script, scriptName)
return sc
}
@ -71,8 +75,8 @@ const api = {
},
load (rootDir) {
scripts = {}
scripts.github = loadScript(readFile(rootDir, 'github.script'))
scripts.google = loadScript(readFile(rootDir, 'google.js'))
scripts.github = loadScript(readFile(rootDir, 'github.script'), 'github')
scripts.google = loadScript(readFile(rootDir, 'google.js'), 'google')
// scripts.jquery = { script: readFile(rootDir, 'jquery.min.js') }
scripts.global = { script: readFile(rootDir, 'global.script') }
return scripts