简体   繁体   English

无法添加事件侦听器

[英]Cannot Add Event Listener

i can add an event listener for clicks to blank but not to twitter in the code below.我可以在下面的代码中添加一个事件侦听器,用于点击blank但不能添加到twitter

const blank = window.open();
const twitter = window.open("https://twitter.com");

const PrintClick = function (name) {
    return function (...args) {
        console.log(name, ...args);
    };
};

blank.addEventListener("click", PrintClick("blank"));
twitter.addEventListener("click", PrintClick("twitter"));

is it because twitter has done something to not let me do this?是不是因为推特做了一些事情不让我这样做? would there be a way to get around it?有没有办法绕过它?

addEventListener 只能监听当前页面的dom对象,可以考虑selenium自动化框架操作

The reason that you did not got any exception :你没有得到任何异常的原因:

Most browsers don't support multiple popups so in order to accomplish it wou need to try using:大多数浏览器不支持多个弹出窗口,因此为了实现它,您需要尝试使用:

window.open(yoururl,"_blank",'PopUp',randomnumber,'scrollbars=1,menubar=0,resizable=1,width=850,height=500');

Or Give each window a new window name .或者 给每个窗口一个新的窗口名称

window.open(url, WindowName)

Security Risk安全风险

You can't add an event listner with different origin using JavaScript, it would be a huge security flaw if you could do it.无法使用 JavaScript 添加具有不同来源的事件侦听器,如果您可以这样做,这将是一个巨大的安全漏洞。 For the same-origin policy browsers block scripts trying to access a frame with a different origin .对于同源策略,浏览器会阻止脚本尝试访问具有不同来源的框架

Origin is considered different if at least one of the following parts of the address isn't maintained:如果地址的以下部分中的至少一个未维护,则认为来源不同:

<protocol>://<hostname>:<port>/...

Protocol , hostname and port must be the same of your domain, if you want to access a frame.如果要访问帧,协议主机名端口必须与您的域相同。

Examples例子

Here's what would happen trying to access the following URLs from http://www.example.com/home/index.html这是尝试从http://www.example.com/home/index.html访问以下 URL 时会发生的情况

URL                                             RESULT 
http://www.example.com/home/other.html       -> Success 
http://www.example.com/dir/inner/another.php -> Success 
http://www.example.com:80                    -> Success (default port for HTTP) 
http://www.example.com:2251                  -> Failure: different port 
http://data.example.com/dir/other.html       -> Failure: different hostname 
https://www.example.com/home/index.html:80   -> Failure: different protocol
ftp://www.example.com:21                     -> Failure: different protocol & port 
https://google.com/search?q=james+bond       -> Failure: different protocol, port & hostname 

Not recommended不建议

Disabling same-origin policy in your browser您的浏览器禁用同源策略

I'll link the relative answer.我会链接相对的答案。 However, please remember that disabling the same-origin policy will only affect your browser .但是,请记住禁用同源策略只会影响您的浏览器 Also, running a browser with same-origin security settings disabled grants any website access to cross-origin resources, so it's very unsafe and should NEVER be done if you do not know exactly what you are doing (eg development purposes) .此外,在禁用同源安全设置的情况下运行浏览器会授予任何网站访问跨源资源的权限,因此这是非常不安全的,如果您不确切知道自己在做什么(例如开发目的),则永远不要这样做

For security reasons browsers disable any interaction across domains that you do not own.出于安全原因,浏览器会禁用您不拥有的域之间的任何交互。 Imagine all the things one could do with that.想象一下人们可以用它做的所有事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM