简体   繁体   English

我的代码在控制台中运行,但不在 Tampermonkey 中

[英]My code was running in console but not in tampermonkey

I was practising coding using JavaScript in console.我正在控制台中使用 JavaScript 练习编码。 it works, but there is a problem in Tampermonkey它有效,但 Tampermonkey 存在问题

function name() {
  var console = document.querySelector('.className');
  let magic = console.childNodes;
  magic[0].innerHTML = '23213';
};
name();

make sure the line确保线路

// @match http://example.com/

is match your target URL与您的目标 URL 匹配

.childNodes return text and tagname so you can't always use .innerHTML , use .children instead. .childNodes返回texttagname名,因此您不能总是使用.innerHTML ,而是使用.children

Please don't use console as variable because it browser variable to output log in your browser请不要使用console作为变量,因为它的浏览器变量为 output 登录您的浏览器

example working script for http://example.com http://example.com的示例工作脚本

// ==UserScript==
// @name         example Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  go to http://example.com/ and see the result
// @author       You
// @match        http://example.com/
// @grant        none
// ==/UserScript==

function name() {
    var div = document.querySelector('div');
    let magic = div.children;
    magic[0].innerHTML = '23213';
    console.log('success!');
};
name();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码可在Firefox控制台中使用,但不能在Tampermonkey中使用? (不是时间问题) - My Code works the in Firefox console but not in Tampermonkey? (not a timing issue) 小书签中的代码可在控制台中使用,但不能在我的Tampermonkey脚本中使用? - Code from bookmarklet works in console but not in my Tampermonkey script? 代码可在浏览器控制台中工作,但不能在tampermonkey中工作 - Code working in browser console but not in tampermonkey 运行本地环境angularJS时,我的开发控制台中的其他代码 - additional code in my dev console when running local environment angularJS 此tampermonkey代码有什么错误? - What is the error with this tampermonkey code? Tampermonkey / Greasemonkey仅在iframe中运行 - Tampermonkey/Greasemonkey only running in iframes 需要jQuery到Tampermonkey脚本和控制台中的安全变量 - require jQuery to a safe variable in Tampermonkey script and console Console.log 在 Tampermonkey 的 setInterval 内不起作用 - Console.log not working inside of setInterval for Tampermonkey 突出显示文本的用户脚本在控制台中有效,但在 Tampermonkey 中无效 - Userscript to Highlight Text works in Console but not Tampermonkey 使用Chrome控制台中的Tampermonkey API? - Use the Tampermonkey API from the Chrome console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM