在 TrustPilot 小部件中自定义 JS/CSS

Pad*_*han 4 html javascript css dom trustpilot

我正在尝试修改我网站上的 TrustPilot 小部件。

TrustPilot 小部件的基本结构是:

#tp-widget_wrapper .tp-widget-wrapper
    #wrapper-top .wrapper-top
        #tp-widget-reviews-wrapper .tp-widget-reviews-wrapper hidden-element
            #tp-widget-reviews .tp-widget-reviews
                .tp-widget-review
                    .tp-widget-stars
                    .date
Run Code Online (Sandbox Code Playgroud)

我试图隐藏 div .date

到目前为止我得到的是:

var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
var date_tags = trustpilot_frame.document.getElementsByClassName("date");
date_tags.style.display = "none";
Run Code Online (Sandbox Code Playgroud)

但是我收到错误:

Uncaught TypeError: Cannot read property 'getElementsByClassName' of undefined
Run Code Online (Sandbox Code Playgroud)

var trustpilot_frame 正在查找 iframe,我只是无法访问其中的任何内容。

编辑

var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
console.log(trustpilot_frame);
var date_tags = trustpilot_frame.getElementsByClassName("date");
console.log(date_tags);
date_tags.style.display = "none";
Run Code Online (Sandbox Code Playgroud)

安慰:

在此输入图像描述

编辑2

var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
var style_tag = trustpilot_frame.createElement("style");
style_tag.textContent = ".date{display:none;}";
trustpilot_frame.getElementsByTagName("head")[0].appendChild(style_tag);
console.log(trustpilot_frame);
Run Code Online (Sandbox Code Playgroud)

安慰:

在此输入图像描述

编辑3

var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0].contentDocument;
trustpilot_frame.onload = setTimeout(hide_dates, 10000);
function hide_dates(){

    // method 1
    var style_tag = trustpilot_frame.createElement("style");
    style_tag.textContent = ".date{display:none;}";
    trustpilot_frame.getElementsByTagName("head")[0].appendChild(style_tag);
    console.log(trustpilot_frame);

    // method 2
    var date_tags = trustpilot_frame.getElementsByClassName("date");
    console.log(date_tags);
    date_tags.style.display = "none";
}
Run Code Online (Sandbox Code Playgroud)

控制台显示与 dom 的 EDIT2 和第一个 EDIT 相同的内容date_tags

https://codepen.io/phallihan/pen/OdwgGd

小智 5

要获取您需要使用的 iframe 文档,document.getElementById('iframe').contentDocument您应该能够使用 getElementsByClassName

https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument

您更新后的代码将如下所示:

var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
var date_tags = trustpilot_frame.contentDocument.getElementsByClassName("date");
date_tags.style.display = "none";
Run Code Online (Sandbox Code Playgroud)

更新

尝试以下方法来等待 iframe 加载。

var trustpilot_children = document.getElementById("trustpilot_widget").childNodes;
var trustpilot_frame = trustpilot_children[0];
trustpilot_frame.onload = function() {
    var date_tags = trustpilot_frame.contentDocument.getElementsByClassName("date");
    date_tags.style.display = "none";
}
Run Code Online (Sandbox Code Playgroud)