简体   繁体   English

chrome扩展中的多个document.onload功能

[英]Multiple document.onload functions in chrome extension

I am developing a chrome extension and i have a requirement as below. 我正在开发chrome扩展程序,并且我有以下要求。

There are 2 content scripts that has to be executed and both the content scripts has document.onload function. 有两个必须执行的内容脚本,并且两个内容脚本都具有document.onload功能。 when the extension is loaded only one among the 2 content scripts are executing. 当加载扩展名时,仅执行两个内容脚本中的一个。

In Detail: say 1.js is a content script that has to be extecuted when xyz.com page is opened. 详细信息:say 1.js是一个内容脚本,当打开xyz.com页面时,该脚本必须被阻止。 2.js is another content script that has to be executed on xyz.com,abc.com,..and so on. 2.js是另一个必须在xyz.com,abc.com等上执行的内容脚本。

Issue : When xyz.com page is loaded, if 2.js gets triggered initailly, content in 1.js is not getting executed and vice versa. 问题:加载xyz.com页面时,如果2.js最初被触发,则1.js中的内容将不会执行,反之亦然。

Is there any workaround for this situation ? 有没有针对这种情况的解决方法?

Please let me know if any other information is needed. 请让我知道是否需要其他信息。

Only one is being triggered because one is overriding the other. 因为其中一个优先于另一个,所以仅触发了一个。 Try using addEventListener instead: 尝试改用addEventListener

document.body.addEventListener('load', function() {
   // your code
});

Setting this multiple times should not override the previous reference like the onLoad function did. 多次设置此值不应像onLoad函数那样覆盖以前的引用。

In your script files, you may need to wrap this in a self invoking function to ensure it is set before the body is loaded. 在脚本文件中,您可能需要将其包装在一个自调用函数中,以确保在加载正文之前已对其进行了设置。

(function() {
    document.body.addEventListener('load', function() { 
        console.log('Inside the 1.js page'); 
        fillDetails(); 
    }); 
})();

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

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