这就是我到目前为止所知道的:
function loadScript(url) {
return new Promise((resolve, reject) => {
let script = document.createElement('script');
script.onload = function() {
resolve();
};
script.src = url;
document.head.appendChild(script);
});
};
loadScript("https://rawgit2.com/icodeforlove/template-colors-web/master/dist-browser/template-colors.js").then(load);
function load() {
function logThing(pkt) {
pkt = pkt.join(', ');
console.log(c`${'['.red.bold}${pkt.green.bold}${']'.red.bold}`);
};
logThing(["Test", "thing", "here"]);
}
通常,在开发工具控制台中,它会这样记录:
但是用tampermonkey记录如下:
为什么袋猴要这么做?我到底该怎么解决这个问题呢?
还请记住,不使用console.log如下所示:
发布于 2021-04-02 20:40:45
改为使用@require
加载脚本:
// ==UserScript==
// @name StackOverflow question 66604679
// @version 0.1
// @author Wolfy
// @match https://stackoverflow.com/questions/66604679/tampermonkey-console-log-doesnt-seem-to-be-the-same-as-the-one-in-chrome-dev-to
// @require https://rawgit2.com/icodeforlove/template-colors-web/master/dist-browser/template-colors.js
// @grant none
// ==/UserScript==
/* globals c */
(function() {
function logThing(wordsArray) {
const stringToPrint = wordsArray.join(',');
console.log(c`${'['.red.bold}${stringToPrint.green.bold}${']'.red.bold}`);
}
logThing(['Test', 'thing', 'here']);
})();
原因是在您的脚本中,<script>
是在页面上下文中添加的,所以它不会修补TM的控制台。
https://stackoverflow.com/questions/66604679
复制相似问题