如何将使用CDATA的Greasemonkey脚本导入Chrome? [英] How do I import this Greasemonkey script, that uses CDATA, to Chrome?

查看:128
本文介绍了如何将使用CDATA的Greasemonkey脚本导入Chrome?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码可以在Greasemonkey上正常工作,但在Chrome中无法正常工作:

  // == UserScript == 
// @name SO
// @namespace stackoverflow.com
// @include * stackoverflow.com / *
// @version 1
// = = / UserScript ==

changeHeaderColor();

函数changeHeaderColor()
{
GM_addStyle((<<![CDATA [
// body {color:white; background-color:黑色}
#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());
}



我需要做些什么才能更改将在Chrome上运行,甚至两者兼而有之?

<><![CDATA [...]]>< /> 代码使用EX4 ,这从来没有得到Chrome的支持,很快就不会被Firefox支持 ,要么

因此,为了使脚本正常工作,您需要为 javascript中的多行字符串。另外,对于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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆