簡體   English   中英

此 chrome 擴展代碼不起作用,我不知道

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

我正在為 Roblox 網頁開發這個小的 chrome 擴展,但出於某種原因,它根本不想正常工作。

錯誤是

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

這是代碼:

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()

我不知道為什么代碼是這樣設置的,但無論如何,有什么地方有問題嗎? 我找不到修復的地方。 謝謝!

getElementsByClassName將始終返回一個類似數組的元素集合——即使只有一個。 因此,在您的代碼中,您不是在集合中的第一個元素上設置style.background屬性,而是嘗試在集合本身上設置它——因為該集合沒有style屬性,您會看到您看到的錯誤。 嘗試將您的代碼更改為:

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();

(我還冒昧地在缺少分號的地方添加了分號,並在不必要的地方刪除了分號)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM