Удалить прослушиватель событий, который был добавлен из расширения Chrome? - PullRequest
0 голосов
/ 27 июня 2019

Я не могу использовать removeEventListener для документа с помощью кнопки в моем расширении Chrome.

Я хорошо прочитал об изолированных мирах, но не могу точно понять, что я должен делать здесь,Я даже не уверен, что всплывающий скрипт также подчинен изолированному контексту мира?

Я проверяю наличие скрытого поля.Когда это скрытое поле обнаружено, код должен удалить прослушиватель событий, но, похоже, его не существует.Это мой popup.js:

function toggle_clare(){      
if(!document.getElementById("clare_on")){ //check for existence of hidden field

var clare_status = document.createElement("input");
    clare_status.type = "hidden";
    clare_status.id = "clare_on";
    clare_status.value = "on";

document.body.appendChild(clare_status); //add it to document if doesn't exist

document.addEventListener("keyup", read_out, true); //add keyup listener

return clare_status.value; //send "on" to the button in the popup
}

else if(document.getElementById("clare_on")){ //alread exists
    var off = "off";

    $("#clare_on").remove();//remove the hidden field

    document.removeEventListener("keyup", read_out, true);//not working

    return off; // send "off" to the button
   }
}

//Function call

document.getElementById( "clare_button" ).addEventListener( "click", 
function(){
    chrome.tabs.executeScript({
        code: '(' + toggle_clare + ')()'
    }, ( results ) => { // receive "on" or "off" from toggle_clare function

            document.getElementById("clare_button").innerText = "Clare is now " + results[0];

    })
})

Я ожидал, что это удалит прослушиватель событий при удалении скрытого поля.ВСЕ этот код работает, кроме removeEventListener.

...