问题描述
我有以下代码可以在Greasemonkey上正常工作,但在Chrome中无法正常工作:
<><![CDATA [...]]>< /> 代码使用,这从来没有得到Chrome的支持,很快就不会被Firefox支持。
因此,为了使脚本正常工作,您需要为。另外,对于Greasemonkey,您应该提供一个 @grant 值,从GM 1.0开始。
用户 \ 转义字符,并要小心和'引号。
另外,不要在这样的字符串中使用 // 注释,因为它们会阻止所有内容,即使它看起来像它在一条新的线上。
它并不漂亮,但是它会这样做:
// == UserScript ==
// @name SO
// @namespace stackoverflow.com
// @include * stackoverflow.com / *
// @version 1
// @grant GM_addStyle
// == / UserScript ==
changeHeaderColor();
function changeHeaderColor(){
GM_addStyle(\
/ * body {color:white; background-color:black} \
* / \
#custom-header {background-color:rgb(251,122,35)} \
\
#nav-questions {background-color:rgb(251,122,35)} \
#nav-tags {background-color:rgb(251,122,35)} \
#nav-users {background-color:rgb(251,122,35)} \
#nav-badges {background -color:rgb(251,122,35)} \
#nav-unanswered {background-color:rgb(251,122,35)} \
#nav-askquestion {background-color:rgb(251,122 ,35)} \
/ * Blau:rgb(0,160,160)rgb(0,200,200)\
* / \
);
}
I've got following code that works fine on Greasemonkey but not in Chrome:
// ==UserScript== // @name SO // @namespace stackoverflow.com // @include *stackoverflow.com/* // @version 1 // ==/UserScript== changeHeaderColor(); function changeHeaderColor() { GM_addStyle((<><![CDATA[ //body { color: white; background-color: black } #custom-header {background-color: rgb(251,122,35)} #nav-questions {background-color: rgb(251,122,35)} #nav-tags {background-color: rgb(251,122,35)} #nav-users {background-color: rgb(251,122,35)} #nav-badges {background-color: rgb(251,122,35)} #nav-unanswered {background-color: rgb(251,122,35)} #nav-askquestion {background-color: rgb(251,122,35)} //Blau: rgb(0,160,160) rgb(0,200,200) ]]></>).toString()); }
What do I have to change so that it will work on Chrome or just even both?解决方案That <><![CDATA[ ... ]]></> code uses "EX4", which was never supported by Chrome and will soon not be supported by Firefox, either.
So, to get that script to work, you need to use a a different method for multiline strings in javascript. Also, for Greasemonkey, you should supply a @grant value, as of GM 1.0.
User the \ escape character and be very careful with " and ' quotes.
Also, do not use // comments in such strings, as they will stop everything after them, even if it looks like it's on a new line.It ain't pretty, but this will do it:
// ==UserScript== // @name SO // @namespace stackoverflow.com // @include *stackoverflow.com/* // @version 1 // @grant GM_addStyle // ==/UserScript== changeHeaderColor (); function changeHeaderColor () { GM_addStyle ( " \ /*body { color: white; background-color: black } \ */ \ #custom-header {background-color: rgb(251,122,35)} \ \ #nav-questions {background-color: rgb(251,122,35)} \ #nav-tags {background-color: rgb(251,122,35)} \ #nav-users {background-color: rgb(251,122,35)} \ #nav-badges {background-color: rgb(251,122,35)} \ #nav-unanswered {background-color: rgb(251,122,35)} \ #nav-askquestion {background-color: rgb(251,122,35)} \ /*Blau: rgb(0,160,160) rgb(0,200,200) \ */ \ " ); }
这篇关于如何将使用CDATA的Greasemonkey脚本导入Chrome?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!