简体   繁体   English

如何在第三方网站上检测JavaScript事件

[英]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: 我的想法:

  • run the site in a headless browser or javascript engine before hand to capture all executed javascript 先手动在无头浏览器或javascript引擎中运行网站以捕获所有已执行的javascript
  • parse the javascript on the fly and hope the third party javascript conforms to my parser's expectations 即时解析javascript,并希望第三方javascript符合我的解析器的期望

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对象,以查看哪些对象具有通过addEventListenerattachEvent安装了常规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. 对于通过onclickonsubmitonfocus等安装的事件处理程序,您可以检查所有现有的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 : 您实际上可以通过更改addEventListenerattachEvent的原型来获得所需的内容:

// 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.

相关问题 使用 javascript 在第三方网站上收集用户统计/日志 - Collecting user stats/logs on third party sites with javascript 如何实现第三方Javascript库? - How to implement Third Party Javascript Library? 如何在javascript中检查第三方cookies是否被阻止 - how to check third party cookies block or not in javascript 为第三方网站构建javascript小部件api时ajax请求的限制。 (例如sharethis,uservoice等) - Limitations of ajax requests when building a javascript widget api for third party sites. (like sharethis, uservoice, etc) JavaScript和第三方Cookie - JavaScript and third party cookies 第三方小部件JavaScript - Third Party Widgets JavaScript 如何在第三方网页中找到绑定到UI事件的功能? - How to find what functions are bound to UI events in a third party webpage? 第三方网站上的JavaScript - JavaScript on third party website 如何使用嵌入式JavaScript在第三方网站上创建弹出窗口? - How to use embedded javascript to create a popup on 3rd party sites? 第三方站点如何检查用户是否同意/通过我的站点,以便他们允许他通过他们的站点? - How third party sites can check if user agreed/pass sth on my site, so they can let him pass sth on their sites?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM