簡體   English   中英

在Chrome擴展程序內容腳本中切換div

[英]Toggling div inside chrome extension content-script

我正在嘗試使用另一個鏈接來切換div,所有這些操作都是通過chrome擴展的內容腳本完成的,但是當我運行解壓后的擴展時,我根本無法切換div ...任何想法?

下面是內容腳本的代碼。

function createHistoryDiv() {
    var divHeight = 97;
    var divMargin = 10;

    var div = document.createElement("div");
        div.id = "history";
    var st = div.style;
    st.display = "block";
    st.zIndex = "10000000";
    st.top = "0px";
    st.left = "0px";
    st.right = "0px";
    st.height = divHeight + "%";
    st.background = "rgba(255, 255, 255, .01)";
    st.margin = divMargin + "px";
    st.padding = "5px";
    st.border = "5px solid black";
    st.color = "black";
        st.fontFamily = "sans-serif";       
        st.fontSize = "36";
    st.position = "fixed";
    st.overflow = "hidden";
    st.boxSizing = "border-box";
        st.pointerEvents = "none";

    document.documentElement.appendChild(div);
    var heightInPixels = parseInt(window.getComputedStyle(div).height);
    st.height = heightInPixels + 'px';
    //document.body.style.webkitTransform = "translateY("
            //+ (heightInPixels + (2 * divMargin))+ "px)";

    return div;
}

function buildDivContent(historyDiv, data) {
    var ul = document.createElement('ul');
    historyDiv.appendChild(ul);

    for (var i = 0, ie = data.length; i < ie; ++i) {
        var a = document.createElement('a');
        a.href = data[i];
        a.appendChild(document.createTextNode(data[i]));

        var li = document.createElement('li');
                li.style.color = "black";
                li.style.display = "inline";
                li.style.wordBreak = "break all";
        li.appendChild(a);
                a.style.color = "black";
                a.style.fontSize = "24px";
                a.style.fontFamily = "sans-serif";
                a.style.linkDecoration = "none";
        ul.appendChild(li);
    }
}

chrome.runtime.sendMessage({ action: "buildTypedUrlList" }, function(data) {
    var historyDiv = createHistoryDiv();
    buildDivContent(historyDiv, data);
});

function logoDiv(){
var div2 = document.createElement("div");
div2.id = "logo";
        var st = div2.style;
    st.display = "block";
    st.zIndex = "10000001";
    st.bottom = "0px";
    //st.left = "0px";
    st.right = "0px";
    //st.height = "42px";
        //st.width = "210px";
    st.background = "rgba(255, 255, 255,1)";
    st.padding = "5px";
        st.margin = "10px";
    st.border = "5px solid black";
    st.color = "black";
        st.fontFamily = "sans-serif";       
    st.position = "fixed";
    st.overflow = "hidden";
    st.boxSizing = "border-box";
        //st.pointerEvents = "none";

            document.documentElement.appendChild(div2);
                div2.innerHTML = div2.innerHTML + "<a href=\"#\" onclick=\"toggle_visibility(\"history\");\" style = \"display:block;font-size:24px;font-family:sans-serif;margin:0;padding:5px;color:black;link-decoration:none;\">TRANSPARENCY</a>";
                //div2.innerHTML = div2.innerHTML + "<p style = \"display:block;font-size:24px;font-family:sans-serif;margin:0;padding:5px;color:black;\">TRANSPARENCY</p>";

                                             return div2;
    function toggle_visibility(id) {
       var e = document.getElementById("history");
       if(e.style.display == "block")
          e.style.display = "hidden";
       else
          e.style.display = "block";
    }

}

chrome.runtime.sendMessage({ action: "buildTypedUrlList" }, function(data){

    var titleDiv = logoDiv();
    buildDivContent(titleDiv);
});

您的代碼存在各種問題(例如, display: hidden是不允許的鍵值對)。

因此,為了發揮作用,內容腳本應如下所示(應刪除以下未包括的所有內容):

function createHistoryDiv() {
    /* No change here. Place the code exactly
     * as it is in the code you posted */
}

function buildDivContent(historyDiv, data) {
    /* No change here. Place the code exactly
     * as it is in the code you posted */
}

function createToggleDiv(historyDiv) {

    /* Create the toggle div */
    var div = document.createElement("div");
    div.textContent = "TRANSPARENCY";

    /* Style the toggle div */
    var st = div.style;
    st.display = "block";
    st.zIndex = "10000001";
    st.bottom = "0px";
    st.right = "0px";
    st.background = "rgb(255, 255, 255)";
    st.padding = "10px";
    st.margin = "10px";
    st.border = "5px solid black";
    st.color = "black";
    st.fontFamily = "sans-serif";
    st.fontSize = "24px";
    st.position = "fixed";
    st.overflow = "hidden";
    st.boxSizing = "border-box";
    st.cursor = "pointer";

    /* Make the toggle div behave 
     * (i.e. toggle `historyDiv` upon 'click') */
    div.addEventListener("click", function(evt) {
        evt.preventDefault();
        var st = historyDiv.style;
        st.display = (st.display == "block") ? "none" : "block";
    });

    /* Insert the toggle div into the DOM */
    document.documentElement.appendChild(div);
}

chrome.runtime.sendMessage({ action: "buildTypedUrlList" }, function(data) {
    var historyDiv = createHistoryDiv();
    var toggleDiv = createToggleDiv(historyDiv);
    buildDivContent(historyDiv, data);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM