Trying to change Steam backgrounds randomly with Tempermonkey, always getting "Parsing Error: Unexpected Token" error

Trying to change Steam backgrounds randomly with Tempermonkey, always getting "Parsing Error: Unexpected Token" error

这里是我的代码

(function() {
    'use strict';

    var imagesArray = [

        "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/583660/361fda7dfb819158079afbf50b7ca13d1ffa6468.jpg",
        "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/203770/c6f5ea583b7883659e35fbedf1b21220dc230dda.jpg"

                  ];

function setBackground(){
    const background = document.querySelector('.profile_background_image_content')
    const randomImage = imagesArray[Math.floor(Math.random() * imagesArray.length + 1)]
    background.style.backgroundImage = url(${randomImage}) !important
}

setBackground()

})();

嗯,老实说,我不知道为什么会出错,或者我做的是正确的。我将不胜感激。

你应该把这个包起来

url(${randomImage}) !important

像这样使用反引号

   `url(${randomImage}) !important`

有效

(function() {
    'use strict';

    document.addEventListener("DOMContentLoaded", function() {

        var imagesArray = [
            "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/583660/361fda7dfb819158079afbf50b7ca13d1ffa6468.jpg",
            "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/items/203770/c6f5ea583b7883659e35fbedf1b21220dc230dda.jpg"
        ];

        const background = document.querySelector('div.profile_page');
        const randomImage = imagesArray[Math.floor(Math.random() * imagesArray.length)];
        background.style.backgroundImage = 'url('+randomImage+')';
        background.style.backgroundColor = 'black';
        background.style.backgroundSize = 'contain';

    });

})();