简体   繁体   English

javascript在页面加载后作为最后一个脚本运行

[英]javascript run as last script after page loaded

I was wondering if it's possible to edit the content of an embedded js which runs and generates dom content.我想知道是否可以编辑运行并生成 dom 内容的嵌入式 js 的内容。 Like for example fb js comments code or analytics code.例如 fb js 评论代码或分析代码。

Is it possible to run a function as the last function that runs on the site so I can be able to make a modification of the rendered content that the embedded script generated.是否可以将一个函数作为在站点上运行的最后一个函数来运行,以便我能够对嵌入脚本生成的渲染内容进行修改。 Let's take an example:让我们举个例子:

<div id="fb-root"></div>
                <script>(function(d, s, id) {
                  var js, fjs = d.getElementsByTagName(s)[0];
                  if (d.getElementById(id)) return;
                  js = d.createElement(s); js.id = id;
                  js.src = \'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2\';
            fjs.parentNode.insertBefore(js, fjs);
        }(document, \'script\', \'facebook-jssdk\'));</script>

When this code renders it generates a comments box, that looks as follows:当此代码呈现时,它会生成一个注释框,如下所示: 在此处输入图片说明

IS it possible to run a script after the content has been generated.是否可以在生成内容后运行脚本。 And lets say modify a span element in the generated output?让我们说修改生成的输出中的跨度元素?

Edit: I'M NOT INTERESTED IN CSS SOLUTIONS.编辑:我对 CSS 解决方案不感兴趣。 I KNOW CSS, I ASKED SPECIFICALLY ABOUT JAVASCRIPT.我知道 CSS,我专门询问了 JAVASCRIPT。 NOT CSS不是 CSS

Can you not just use css to do that?你不能只使用 css 来做到这一点吗? !important can help in these situations... !important可以在这些情况下提供帮助...

If not, you could use the jQuery Live Query plugin but i think it is a bit outdated...如果没有,您可以使用jQuery Live Query 插件,但我认为它有点过时了...

$('.theElementSelector').livequery(function() {
  //your code to change appearance of controls
});

Or you could use DOMNodeInserted (which is also outdated/deprecated):或者您可以使用DOMNodeInserted (它也已过时/已弃用):

$('body').on('DOMNodeInserted', '.theElementSelector', function(e) {
  //your code to change appearance of controls
});

A better solution is to use MutationObserver .更好的解决方案是使用MutationObserver

Another is insertionQuery :另一个是插入查询

insertionQ('theElementSelector').every(function(element){
    //callback
});

If you can work out what className or CSS selector for the parts of the Facebook you want to change.如果你能找出你想要更改的 Facebook 部分的 className 或 CSS 选择器。 You can directly change the stylesheet for these.您可以直接更改这些样式表。

Below is a little snippet that demonstrates this.下面是一个演示这一点的小片段。 If we pretend the classname .test is some CSS selector on this Facebook plugin, you could change the color like below.如果我们假设类名.test是这个 Facebook 插件上的某个 CSS 选择器,你可以像下面一样更改颜色。 The setInterval is of course not needed, it's only there to show that it's changing in real time. setInterval 当然不是必需的,它只是为了表明它正在实时变化。

 let cssEntry; for (const s of document.styleSheets) { for (const r of s.cssRules) { if (r.selectorText === ".test") { cssEntry = r; } } } let c = 0; const colors = ["red", "green", "blue"]; setInterval(() => { cssEntry.style["background-color"] = colors[(c++)%3]; }, 1000);
 .test { color: white; background-color: black; }
 <div class="test"> <p>This should be white text on black background<p> <p>But in a few seconds should change between red / green / blue every second by directly changing the CSS inside the loaded stylesheet</p> </div>

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

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