Content.js 不能在扩展中工作,但在 chrome 控制台中工作

我正在构建一个扩展程序,用于获取 youtube 播放列表的总时间。它可以获取播放列表的视频元素,但无法查询每个视频元素以获取时间,因为它返回一个空元素,但相同的代码在 Chrome 控制台中运行良好。

有问题的函数是 getTime(),而行是 timeStringElement = video.querySelector('span#text');

function getTime(){
    let videos = document.querySelectorAll('#playlist-items');
    videos[videos.length-1].scrollIntoView(true);
    let timeHours = 0;
    let timeStringElement;
    let containerElement;
    for (let video of videos){
        timeStringElement = video.querySelector('span#text');
        if (timeStringElement !== null){
            console.log(timeStringElement.innerText);
            timeHours += getHours(timeStringElement.innerText);
        }
    }
    return timeHours;
}
function getHours(time){
    let timeList = time.split(':').reverse();
    let timeHours = 0.0;
    let multiplier = 1/3600;
    for (let timeValue of timeList){
        timeHours += timeValue*multiplier;
        multiplier *= 60;
    }
    return timeHours;
}

window.onload = function(){
    let url = window.location.href;
    if (url.indexOf('list=') !== -1){
        let hours = getTime();
        console.log(hours);
    }
}
yzhwyf 回答:Content.js 不能在扩展中工作,但在 chrome 控制台中工作

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/10901.html

大家都在问