JS / CSS-自定义trustpilot小部件

时间:2019-02-13 10:20:59

标签: javascript html 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

我正试图隐藏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";

但是我得到了错误:

Uncaught TypeError: Cannot read property 'getElementsByClassName' of undefined

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";

控制台:

enter image description here

// ----- EDIT2 ----- //

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);

控制台:

enter image description here

// ----- EDIT3 ----- //

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";
}

控制台对dom显示与EDIT2相同,对date_tags显示第一个EDIT

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

2 个答案:

答案 0 :(得分:3)

要获取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";

更新

请尝试以下操作以等待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";
}

答案 1 :(得分:0)

我不想这样做,但是我是一个低下的开发人员,老板坚持要这样做。我无法通过JS / CSS执行此操作,因此最终不得不用自己的滑块替换该代码段并将评论另存为图像。

相关问题