我想听听我网站上发生的所有事件。 我使用monitorEvents()命令,但此命令仅将事件打印到控制台,我想设置一个回调函数,以便对该数据进行一些操作而不将其打印到控制台。
有没有聪明的方法可以做到这一点?
答案 0 :(得分:2)
您可以尝试这样的事情:
var eventsList = ["mousedown", "mouseup", "click", "dblclick", "mousemove",
"mouseover", "mouseout", "mousewheel", "keydown", "keyup", "keypress",
"textInput", "touchstart", "touchmove", "touchend", "touchcancel", "resize",
"scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset"];
var callbackFunction = function(element, eventName) {
console.log('Triggered event ' + eventName);
};
var elements = document.querySelectorAll('*');
for (var i = 0; i < elements.length; i++) {
for (var j = 0; j < eventsList.length; j++) {
var element = elements[i];
var event = eventsList[j];
element.addEventListener(event,
callbackFunction.bind(this, element, event), true);
}
}
答案 1 :(得分:1)
我找到了最好的方法。 此代码基于原始monitorEvents()命令。 使用此脚本,可以收听所有Chrome事件并使用自定义功能处理它们......您只需编辑“eventHandler”功能即可。 要试一试,只需复制到Chrome控制台,然后使用:startMonitorEvents(window,eventHandler);
/**
* @param {Event} event
*/
function eventHandler(event)
{
// Do whatever you want here...
console.log("my custom handler… ", event.type, event);
}
/**
* @param {Object} object
* @param {Function} callback
*/
function startMonitorEvents(object, callback)
{
if (!object || !object.addEventListener || !object.removeEventListener)
return;
var types = getEventsForMonitor();
for (var i = 0; i < types.length; ++i) {
object.removeEventListener(types[i], callback, false);
object.addEventListener(types[i], callback, false);
}
}
function getEventsForMonitor()
{
var result = [];
// mouse
result.splice(0, 0, "mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel");
// key
result.splice(0, 0, "keydown", "keyup", "keypress", "textInput");
// touch
result.splice(0, 0, "touchstart", "touchmove", "touchend", "touchcancel");
// control
result.splice(0, 0, "resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset");
// others
result.splice(0, 0, "load", "unload", "abort", "error", "select", "change", "submit", "reset", "focus", "blur", "resize", "scroll", "search", "devicemotion", "deviceorientation");
return result;
}
// Usage: startMonitorEvents(window, eventHandler);
答案 2 :(得分:0)
Firebug内部使用Paul Rad的解决方案,其中包含以下事件列表:
["DOMActivate", "DOMAttrModified", "DOMCharacterDataModified", "DOMFocusIn", "DOMFocusOut", "DOMNodeInserted", "DOMNodeInsertedIntoDocument", "DOMNodeRemoved", "DOMNodeRemovedFromDocument", "DOMSubtreeModified", "abort", "beforeunload", "blur", "change", "click", "composition", "compositionend", "compositionstart", "contextmenu", "copy", "cut", "dblclick", "dragdrop", "dragenter", "dragexit", "draggesture", "dragover", "error", "focus", "input", "keydown", "keypress", "keyup", "load", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup", "overflow", "overflowchanged", "paint", "paste", "reset", "resize", "scroll", "select", "submit", "text", "touchcancel", "touchend", "touchenter", "touchleave", "touchmove", "touchstart", "underflow", "unload"]