简体   繁体   English

此 chrome 扩展代码不起作用,我不知道

[英]This code for a chrome extension is not working, and I have no Idea

I am working on this small chrome extension for the Roblox web page, and for some reason, it doesn't want to work properly at all.我正在为 Roblox 网页开发这个小的 chrome 扩展,但出于某种原因,它根本不想正常工作。

the error is错误是

Uncaught TypeError: Cannot set property 'background' of undefined
at extension_update (themeManager.js:7)
at themeManager.js:14

and this is the code:这是代码:

var colorOnePath = document.getElementsByClassName("navbar-fixed-top rbx-header");
var colorTwoPath = [document.getElementsByTagName("body"), document.getElementsByClassName("content")];    

var color = ["#008919", "#000000"];

function extension_update() {
    colorOnePath.style.background = color[0];
    colorTwoPath[0].style.background = color[1];
    colorTwoPath[1].style.background = color[1];

    setTimeout(1000, extension_update)
};

extension_update()

I have no idea why the code is set up like this, but anyway, is there a problem anywhere?我不知道为什么代码是这样设置的,但无论如何,有什么地方有问题吗? I couldn't find where to fix.我找不到修复的地方。 thx!谢谢!

getElementsByClassName will always return an array-like collection of elements-- even if there is only one. getElementsByClassName将始终返回一个类似数组的元素集合——即使只有一个。 So in your code, instead of setting the style.background property on the first element in the collection, you're attempting to set it on the collect itself -- as the collection has no style property, you get the error you are seeing.因此,在您的代码中,您不是在集合中的第一个元素上设置style.background属性,而是尝试在集合本身上设置它——因为该集合没有style属性,您会看到您看到的错误。 Try changing your code to this:尝试将您的代码更改为:

var colorOnePath = document.getElementsByClassName("navbar-fixed-top rbx-header")[0];
var colorTwoPath = [document.getElementsByTagName("body")[0], document.getElementsByClassName("content")[0]];    

var color = ["#008919", "#000000"];

function extension_update() {
    colorOnePath.style.background = color[0];
    colorTwoPath[0].style.background = color[1];
    colorTwoPath[1].style.background = color[1];

    setTimeout(1000, extension_update);
}

extension_update();

(I also took the liberty of adding semi-colons where they were missing and removing them where they were unnecessary). (我还冒昧地在缺少分号的地方添加了分号,并在不必要的地方删除了分号)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM