问题描述
我使用Greasemonkey并尝试在特定域中添加规则。但是它导致一个错误,说该操作是不安全的
。
该代码在Chrome上正常工作。
I'm using Greasemonkey and trying to add a rule in a specific domain. But it results in an error saying The operation is insecure
.
The code works fine on Chrome.
脚本在 http://mydomain.com/test/test.php
上运行,CSS文件 http://cdn.mydomain.com/test/css/global.css
我的功能:
function css(selector, property, value) {
for (var i=0; i<document.styleSheets.length;i++)
{
try
{
document.styleSheets[i].insertRule(selector+ ' {'+property+':'+value+'}', document.styleSheets[i].cssRules.length);
}
catch(err)
{
try // IE
{
document.styleSheets[i].addRule(selector, property+':'+value);
}
catch(err) {}
}
}
}
在Google上,我发现可能是因为我要访问跨域,所以我试图将CSS文件的URL添加到接受的URL,但没有结果。
On Google I found that it could be because I'm trying to access cross-domains, so I've tried adding the URL to the CSS file to the 'accepted URLs' but no result.
如何解决此问题?
推荐答案
访问跨域的样式表。它可以(或至少用于)抛出异常:
Yes, Firefox blocks access to stylesheets that are cross-domain. It can (or at least used to) throw the exception:
$ b b
但是,使用CSS,您不需要向特定样式表添加规则。
But, with CSS, you don't need to add rules to a specific style sheet. Just overwrite the style you care about.
例如,如果页面设置:
body {
background: white;
}
您的脚本集:
body {
background: red;
}
那么页面将是红色的(名义上)。
Then the page will be red (nominally).
要更改目标页面样式的最简单,最聪明的方法,请参阅。
For the easiest, smartest way to change target page styles, see previous answers like this one.
这篇关于尝试向CSS样式表添加规则给出“操作不安全”在Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!