[英]How to detect javascript events on third party sites
Is there any way (implemented or just in theory) that one could detect javascript events and actions assigned on a third party site? 是否有任何方法(实施的或仅是理论上的)可以检测到在第三方站点上分配的JavaScript事件和操作?
If I am injecting javascript into a foreign site and want to avoid overwriting onsubmit, onclick, onfocus and other such events, is there any way to detect and extend such callbacks rather than overwrite them? 如果我将javascript注入到外部站点中并希望避免覆盖onsubmit,onclick,onfocus和其他此类事件,是否有任何方法可以检测和扩展此类回调,而不是覆盖它们?
ideas I've had: 我的想法:
If you just want to add new event handlers without overwriting the existing event handlers, you can use addEventListener()
or attachEvent()
(for older versions of IE) instead of setting .onclick
, .onsubmit
, etc... to add a new event handler without affecting the previous event handlers. 如果只想添加新的事件处理程序而不覆盖现有的事件处理程序,则可以使用
addEventListener()
或attachEvent()
(用于IE的较早版本),而不是设置.onclick
, .onsubmit
等...来添加新的事件处理程序。事件处理程序,而不会影响以前的事件处理程序。
Here's a simple cross browser function to add an event: 这是添加事件的简单跨浏览器功能:
// add event cross browser
function addEvent(elem, event, fn) {
if (elem.addEventListener) {
elem.addEventListener(event, fn, false);
} else {
elem.attachEvent("on" + event, function() {
// set the this pointer same as addEventListener when fn is called
return(fn.call(elem, window.event));
});
}
}
There is no cross browser way that I'm aware of from regular javascript to interrogate existing DOM objects to see which ones have regular javascript event handlers installed via addEventListener
or attachEvent
. 我不知道从常规javascript询问现有的DOM对象,以查看哪些对象具有通过
addEventListener
或attachEvent
安装了常规javascript事件处理程序的跨浏览器方式。 If the event handlers are installed with jQuery, they can be interrogated via this technique . 如果事件处理程序与jQuery一起安装,则可以通过此技术来查询它们。
There is some future spec work that has been done to add a property that would list all the event handlers, but I am not sure it is implemented yet. 将来还会做一些规范工作,以添加将列出所有事件处理程序的属性,但是我不确定它是否已实现。 You can read more about that here .
您可以在此处阅读更多有关此内容的信息 。
For event handlers installed with onclick
, onsubmit
, onfocus
, etc..., you can check all existing DOM elements to see which ones have handlers assigned for those events and you can then hook them if you want to. 对于通过
onclick
, onsubmit
, onfocus
等安装的事件处理程序,您可以检查所有现有的DOM元素,以查看为那些事件分配了处理程序的元素,然后可以根据需要进行挂钩。
// add other events here you are interested in
var events = ["onclick", "onsubmit", "onfocus", "onblur"];
var elems = document.getElementsByTagName("*"), item;
for (var i = 0; i < elems.length; i++) {
item = elems[i];
for (var j = 0; j < events.length; j++) {
if (item[events[j]]) {
console.log("event handler for " + events[j]);
}
}
}
You can actually get what you want by changing the prototype of addEventListener
and attachEvent
: 您实际上可以通过更改
addEventListener
和attachEvent
的原型来获得所需的内容:
// intercept events cross browser
var method;
if (HTMLElement.prototype.addEventListener) {
method = 'addEventListener';
} else {
method = 'attachEvent';
}
HTMLElement.prototype.realAddEventListener = HTMLElement.prototype[method];
HTMLElement.prototype[method] = function(a,b,c) {
console.log('here are all the events being added:', arguments);
this.realAddEventListener(a,b,c);
};
Of course, it's important that this is added into the page before any other page scripts, but it can come after or before libraries like jQuery, but might have conflicts on the prototype with prototype.js, not sure. 当然,将其添加到页面中的任何其他页面脚本之前是很重要的,但是它可以在jQuery之类的库之后或之前,但是不确定与prototype.js在原型上是否有冲突。
You'll also want to check for onclicks
and the whole set of events, which you can find by running console.dir(document.createElement('a'))
in a javascript console. 您还需要检查
onclicks
和整个事件集,可以通过在JavaScript控制台中运行console.dir(document.createElement('a'))
来查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.