为什么重新加载2次后内容脚本无法继续执行?

时间:2018-11-19 12:04:58

标签: javascript google-chrome google-chrome-extension google-chrome-devtools

我一直在扩展,以确保始终能在www.frikitrivial.com上获胜。所有机制都在内容脚本文件中,每次选项卡的url等于“ struct padding”时,都必须重新加载页面,但是经过2次后,它停止了,我的意思是内容脚本不再执行。我已经检查了在脚本开始处编写console.log()的方法。

谢谢。

这是内容脚本文件中的代码(您可以忽略其中的大部分内容)。

console.log( "Iniciando partida..." )


let question, answers, _answers

if( location.href !==  "http://www.frikitrivial.com/end.php" ){
    question = document.querySelectorAll("div.question")[0].textContent
    answers = []
    _answers = document.querySelectorAll("a.answer")
    
    document.querySelectorAll("a.answer").forEach( e => answers.push( e.textContent ) )
}


function click( s ) {
    let c
    for( let i = 0; i < 4; i++ ) {
        if( s === _answers[i].textContent ) {
            c = _answers[i]
        }
    }
    console.log( "El siguiente elemento va a ser clickado..." )
    console.log( c )
    c.click()
}

chrome.storage.sync.get(["lastClicked"], function(result){
    if( result.lastClicked ) {
        console.log( "Leido lastClicked..." )
        if( location.href === "http://www.frikitrivial.com/end.php" ) { // manejamos fallo
            console.log( "Manejando fallo de pregunta..." )
            chrome.storage.sync.set({
                [result.lastClicked.question]: {
                    question: result.lastClicked.question,
                    answers: result.lastClicked.answers.shift(),
                    answered: false
                }
            }, () => chrome.runtime.sendMessage({redirect: "https://www.frikitrivial.com/game.php"}))
        } else { // actualizamos el estado de la ultima pregunta respondida
            if( !result.lastClicked.answered ){
                console.log( "Manejando lastClicked not answered..." )
                chrome.storage.sync.set({
                    [result.lastClicked.question]: {
                        question: result.lastClicked.question,
                        answers: result.lastClicked.answers,
                        answered: true
                    }
                }, answerNewQuestion)
            } else {
                console.log( "Manejando lastClicked answered..." )
                answerNewQuestion()
            }
        }
    } else {
        answerNewQuestion()
    }
})

function answerNewQuestion() {
    chrome.storage.sync.get([question], function(result){
        if( result.question ) { // si ya hemos intentado responder esta pregunta alguna vez
            if( result.question.answered ) {
                chrome.storage.sync.set( {
                    [result.question]: {
                        question: result.question.question,
                        answers: result.question.answers,
                        answered: result.question.answered
                    }
                }, () => click( result.question.answers[0] ) )
            } else {
                chrome.storage.sync.set( {
                    lastClicked: {
                        question: result.question.question,
                        answers: result.question.answers,
                        answered: result.question.answered
                    }
                }, () => click( result.question.answers[0] ) )
            }
        } else {
            chrome.storage.sync.set( {
                [question]: {
                    question: question,
                    answers: answers,
                    answered: false
                }
            }, function( result ){
                chrome.storage.sync.set( {
                    lastClicked: {
                        question: question,
                        answers: answers,
                        answered: false
                    }
                }, () => click( answers[0] ) )
            })
        }
    })  
}

这是manifest.json!

{
"name": "Getting Started Example",
"version": "1.0",
"description": "Build an Extension!",
"permissions": [
    "activeTab", 
    "declarativeContent", 
    "storage",
    "unlimitedStorage"
],
"background": {
    "scripts": ["background.js"],
    "persistent": true
},
"page_action": {
    "default_popup": "popup.html",
    "default_icon": {
        "16": "images/get_started16.png",
        "32": "images/get_started32.png",
        "48": "images/get_started48.png",
        "128": "images/get_started128.png"
    }
},
"icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
},
"content_scripts": [
    {
    "matches": ["http://www.frikitrivial.com/game.php*", "http://www.frikitrivial.com/end.php"],
    "js": ["contentScript.js"]
    }
],
"manifest_version": 2

}

0 个答案:

没有答案