Chrome ext , content script that creates JQuery di

2019-09-10 15:28发布

i have simple chrome extension that opens JQuery dialog box on each tab i open ,
the problem is when web page has iframe in it , the dialog box opens as many iframes are in the page .
i want to avoid this , all i need it open only and only 1 instance of the Dialog box for each page . how can i avoid the iframes in the page ?
this is my content script :

var layerNode= document.createElement('div');
    layerNode.setAttribute('id','dialog');
    layerNode.setAttribute('title','Basic dialog');
var pNode= document.createElement('p');
    console.log("msg var: "+massage); 
    pNode.innerHTML  = massage;

layerNode.appendChild(pNode);
document.body.appendChild(layerNode);

jQuery("#dialog").dialog({
    autoOpen: true, 
    draggable: true,
    resizable: true,
    height: 'auto',
    width: 500,
    zIndex:3999,
    modal: false,
    open: function(event, ui) {
        $(event.target).parent().css('position','fixed');
        $(event.target).parent().css('top', '5px');
        $(event.target).parent().css('left', '10px');
    }

});

2条回答
SAY GOODBYE
2楼-- · 2019-09-10 16:13

How about wrapping all this with

if(window==window.top) {
   // we're not in an iframe
   // your code goes here
}
查看更多
爷、活的狠高调
3楼-- · 2019-09-10 16:16

As PAEz said in a comment, content scripts run in only the top frame by default. To make them run in subframes, you'd need to have '"all_frames":true' in the content_scripts section of your manifest. Similarly, if you're injecting script using tabs.executeScript, you'd have to include '"allFrames":true' in the InjectDetails argument.

查看更多
登录 后发表回答