Getting jQuery and GM_addStyle to work in a Chrome userscript based off of a working Greasemonkey script

前端 未结 1 541
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 12:19

I wrote a simple Greasemonkey script that enlarges thumbnail pictures in a flyover popup. It uses a lot of jQuery in it. It works just fine on Firefox. But not on Chrome since i

相关标签:
1条回答
  • 2021-01-22 12:55

    Two main problems and 1 possible problem:

    1) Do not wrap GM_addStyle() inside the main() function. GM_addStyle() only works in script scope, it will not work injected to the target page (which is what that main() and addJQuery() business does).

    2) The current code uses E4X to make a multiline string to send to GM_addStyle(), but Chrome doesn't support E4X.

    Alas, the multiline string hack that Chrome does support (for now) does not work in Firefox.

    That means it's slightly harder to code realistic styles with GM_addStyle() if you wish to support both browsers. Use the multiline escape character (\) like so:

    GM_addStyle ( "                                 \
        #idLargePicturePopupWindow  {               \
            position:               absolute;       \
            background:             white;          \
            border:                 none;           \
            margin:                 1ex;            \
            opacity:                1.0;            \
            z-index:                1222;           \
            min-height:             100px;          \
            min-width:              200px;          \
            padding:                0;              \
            display:                none;           \
            top:                    2em;            \
            left:                   50em;           \
        }                                           \
        #idLargePicturePopupWindow img  {           \
            margin:                 0;              \
            margin-bottom:          -4px;           \
            padding:                0;              \
        }                                           \
    " );
    

    ¿3?) That particular version of addJQuery() may not always work (race condition). Let me know if it doesn't.

    0 讨论(0)
提交回复
热议问题