简体   繁体   English

如何防止CSS影响某些元素?

[英]How can I prevent CSS from affecting certain element?

I am writing a GreaseMonkey script that sometimes creates a modal dialog – something like 我正在编写一个GreaseMonkey脚本,该脚本有时会创建一个模态对话框–类似

<div id="dialog">
   Foo
</div>

. But what can I do if the site has something like 但是,如果该网站有类似的内容,该怎么办

#dialog {
    display: none !important;
}

? Or maybe the owner of some site is paranoid and has something like 或者,某些网站的所有者偏执狂,并且拥有类似

div {
    display: none !important;
}
div.trusted {
    display: block !important;
}

because he doesn't want people like me adding untrusted content to his page. 因为他不希望像我这样的人在他的页面上添加不受信任的内容。 How can I prevent those styles from hiding my dialog? 如何防止这些样式隐藏对话框?

My script runs on all pages, so I can't adapt my code to each case. 我的脚本在所有页面上运行,因此我无法将代码适应每种情况。

Is there a way to sandbox my dialog? 有没有办法将对话框沙箱化

Actually a very interessting problem, here is another approach: 实际上是一个非常有趣的问题,这是另一种方法:

adding an iframe and modifying it creates a seperate css space for you (your sandbox) 添加iframe并对其进行修改会为您创建一个单独的CSS空间(您的沙箱)

look at this jsfiddle example: http://jsfiddle.net/ZpC3R/2/ 看看这个jsfiddle示例: http : //jsfiddle.net/ZpC3R/2/

var ele = document.createElement("iframe");
ele.id = "dialog";
ele.src = 'javascript:false;';
ele.style.height = "100px";
ele.style.width = "300px";
ele.style.setProperty("display", "block", "important");
document.getElementById("dialog").onload = function() {
    var d = document.getElementById("dialog").contentWindow.document;
    // ... do your stuff within the iframe
};

this seems to work without problem in firefox. 这似乎在Firefox中没有问题的工作。

now you only have to make sure that the iframe is untouched, you can do this they way i described in my 1. answer 现在,您只需要确保iframe未被触碰,就可以按照我在1.答案中描述的方式进行操作。

just create the div like this: 像这样创建div:

var ele = document.createElement("div");
ele.style.setProperty("display", "block", "important");

that should overwrite all other styles afaik. 应该会覆盖所有其他样式的afaik。

look here, it seems to work: http://jsfiddle.net/ZpC3R/ 看看这里,它似乎可以工作: http : //jsfiddle.net/ZpC3R/

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

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