简单的添加事件侦听器不起作用,我不知道如何解决它



所以,我对Javascript(在控制台中(中有多少浏览器输出错误不满意,因此我继续为任何网站编写了一个简单的控制台插件,它可以执行任何带有一些附加功能的功能,例如循环函数。 要提交命令以执行,您只需按回车键。这就是问题所在:

我的"插件"阻止任何其他事件侦听器工作,而不是它自己的。 我不知道我做错了什么,但我担心这与我的一些事件侦听器有关!!

这是代码。

用JavaScript编写!

window.onload = function() {
var isIE = /*@cc_on!@*/ false || !!document.documentMode;
document.querySelector("body").innerHTML += "<div style='border:1px solid black; width: 400px; height:300px;overflow:auto;background-color:rgb(29, 29, 29); color:rgb(10, 160, 160);' id='consoleoutput'></div><div id='speed'></div>";
if ( /*@cc_on!@*/ false || !!document.documentMode || !isIE && !!window.StyleMedia) {
document.querySelector("body").innerHTML += "<input style='border:1px solid black; width: 357px;background-color:rgb(40, 40, 40); color:rgb(300, 300, 300);' type='text' placeholder='type your commands/functions here' id='consoleinput'></input><span>loop</span><input type='checkbox' id='loopini'>";
} else {
document.querySelector("body").innerHTML += "<form action='#' id='thefasdasd'><input style='border:1px solid black; width: 357px;background-color:rgb(40, 40, 40); color:rgb(300, 300, 300);' type='text' placeholder='type your commands/functions here' id='consoleinput'></input><span>loop</span><input type='checkbox' id='loopini'></form>";
}
if (navigator.userAgent.indexOf("Firefox") != -1) {
window.location.href = "#";
} else {
window.location.href = "?#";
}
if (isIE) { document.write("Internet Explorer is not supported please use any other browser") }
document.querySelector("#consoleinput").addEventListener('keypress', function(e) {
var key = e.which || e.keyCode;
if (key === 13) {
if ($('input[id=loopini]').prop('checked')) {
var inh = document.getElementById("consoleinput").value;
loop(inh);
} else {
var inh = document.getElementById("consoleinput").value;
executeScript(inh);
}
}
})
};
if (document.getElementById("speed").innerHTML == "") {
document.getElementById("speed").innerHTML = 600;
}
function changeloopspeed(speed) {  document.getElementById("speed").innerHTML = speed; }
function loop(b) {
sayb("Gib 'changeloopspeed(die neue Geschwindigkeit in ms)' ein um die Geschwindigkeit des loops zu ändern!")
var a = document.getElementById("speed").innerHTML;
var timedconsolefunc = function() {
var asdas = $('input[id=loopini]').prop('checked');
executeScript(b);
if (asdas) {
setTimeout(timedconsolefunc, a);
}
}
setTimeout(timedconsolefunc, a);
}
function say(a) {
if (a != undefined) {
document.getElementById("consoleoutput").innerHTML += "<p style='color:rgb(48, 243, 96)'>" + a + "</p>";
scrollTo();
}
}
function saya(a) {
document.getElementById("consoleoutput").innerHTML += "<p style='color:rgb(10, 160, 160)'>" + a + "</p>";
scrollTo();
}
function sayb(a, b) {
document.getElementById("consoleoutput").innerHTML += "<p style='color:rgb(100, 160, 160)'> " + a + " <span style='color:rgb(48, 243, 96)'> " + b + "</span></p>";
scrollTo();
}
function saye(a) {
document.getElementById("consoleoutput").innerHTML += "Error: <p style='color:rgb(999, 0, 0)'>" + a + "</p>";
scrollTo();
}
function executeScript(source) {
var script = document.createElement("script");
script.onload = script.onerror = function() { this.remove(); };
script.src = "data:text/plain;base64," + btoa("function asdfasd() {try{" + source + "}catch(error) {saye(error.message)}}; var exer = asdfasd(); if (exer != undefined){sayb('Returned value:',exer)} else {saya('no \'return \' was found')}");
document.body.appendChild(script);
document.getElementById("consoleoutput").innerHTML += "code Executed!<br/>"
document.getElementById("consoleinput").value = "";
}
var scrollTo = function(elem) {
var elem = document.getElementById("consoleoutput");
elem.scrollTop = elem.scrollHeight;
console.log("test");
return this;
};

它是否与以下方面有关:

window.onload = function(( }

???!!! 请帮忙。

当我尝试使代码更小以进行演示时,问题总是消失。 所以我所知道的是,在上面的代码中存在问题,我找不到它!!

有没有人知道如何解决这个问题?!

我不知道,我做错了什么。

你的丹尼斯

当涉及到事件侦听器时,您的代码似乎中断的唯一方式似乎是当它覆盖window.onload = ...等介质中的现有侦听器时,在这种情况下,它应该window.addEventListener("load", ...)

最新更新