2

我有以下代码在 Greasemonkey 上运行良好,但在 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());
}


我必须进行哪些更改才能使其在 Chrome 上运行,甚至两者都运行?

4

1 回答 1

4

<><![CDATA[ ... ]]></>代码使用“EX4”,Chrome 从未支持过,Firefox 也将很快不再支持

因此,要使该脚本正常工作,您需要对javascript 中的多行字符串使用不同的方法。此外,对于 Greasemonkey,您应该提供一个@grant值,从 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)                       \
        */                                                          \
    " );
}
于 2012-11-27T22:48:31.603 回答