Asked  6 Months ago    Answers:  5   Viewed   124 times

Ever since the new Greasemonkey 1.0 was released a few days ago, every site that has jQuery and where I use jQuery in my Greasemonkey script do not run my script properly. The jQuery I have in my GS script (using the @require metadata) conflicts with the page's jQuery. This is due to the new @grant code.

I've read the documentation but still don't know how to run GS scripts in a sandbox again; the only options seem to be to either grant access to a GS API or to grant it to none and run the script without any security limitations, which doesn't work at all for me when I've designed my dozens of GS scripts to run WITH security limitations and like it that way.

 Answers

55

Greasemonkey 1.0, radically changed the way the sandbox works, busting thousands of scripts. This is a huge problem, and I hope you will join me in voicing your opinion/experiences on the principle bug report for this issue.

The Greasemonkey blog claims that you can workaround the issue with the following:

this.$ = this.jQuery = jQuery.noConflict(true);

... Which I'm not sure will work in all cases. And it is the exact wrong approach from a side-effects-avoiding, DRY-principle, atomic-coding philosophy.   In my opinion, the best strategy is to restore the sandbox.

Reactivate the sandbox by specifying a @grant value (other than none). Edit your Metadata Block to end with the following lines:

// @grant       GM_addStyle
// @grant       GM.getValue
// ==/UserScript==
/*- The @grant directive is needed to work around a design flaws introduced in GM 1.0
    and again in GM 4.0.
    It restores the sandbox.
*/

The sandbox will be restored and all conflicts will be resolved.
And the scripts will be compatible with superior engines like Tampermonkey and Violentmonkey.

Tuesday, June 1, 2021
 
mopsyd
answered 6 Months ago
96

I had the same problem as described in the question. That's why I came up with the following solution:

Include the primefaces built-in jQuery library (currently 1.4.1) as including an own jQuery library leads to CSS formatting problems. Adding the target="head" attribute allows for specifying the tag everywhere - e.g. when using templating you not always have access to the <head> tag:

<h:outputScript library="primefaces" name="jquery/jquery.js" target="head" />

The primefaces jQuery library is included by default in conflict mode. That means the $() shortcut cannot by used. To overcome this issue include the following line in a <script> or <h:outputScript> tag:

<h:outputScript target="head">
    // Add the $() function
    $ = jQuery;
    // Now you can use it
    $(document).ready(function() {
        ...
    });
</h:outputScript>

That's the best solution I could dig out so far, using primefaces 2.2.1.

Wednesday, June 2, 2021
 
cusejuice
answered 6 Months ago
85

It may be that the JQuery file can't be found, try this for the script reference:

<script src="<%= Url.Content ("~/Scripts/jquery-1.2.6.js") %>" type="text/javascript"></script>

The Url.Content will build the correct path regardless of whether the app is running in the root or a sub-directory.

Also, if you've installed the hot-fix for the JS intellisense, you can use this in addition to the above:

<% if (false) { %>
    <!-- Don't wrap this is a Url.Content call, it's like this so we get intellisense! -->
    <script src="../../Scripts/jquery-1.2.6-vsdoc.js" type="text/javascript"></script>
<% } %>

Edit:

Since the release of the RC 1 Refresh, there's been a know bug about placing elements with code blocks in the header, Philip Haacked has a nice article about solving it...

Edit 2:

Apparently this has been fixed since RC 2 was released...

• Code nuggets that are direct children of the head element do not cause an exception if the runat="server" attribute is added to them.

Edit 3:

The hot-fix referenced earlier is only applicable to VS2008 and is available here - check out the blog post by the VS Web Dev Team here for details. VS2010 has it built in.

Saturday, September 4, 2021
 
ilivewithian
answered 3 Months ago
48

Greasemonkey (and Tampermonkey) has built-in support for cross-domain AJAX. Use the GM_xmlhttpRequest function.

Here's a complete userscript that illustrates the process:

// ==UserScript==
// @name        _Starter AJAX request in GM, TM, etc.
// @match       *://YOUR_SERVER.COM/YOUR_PATH/*
// @grant       GM_xmlhttpRequest
// @connect     targetdomain1.com
// ==/UserScript==

GM_xmlhttpRequest ( {
    method:     'GET',
    url:        'http://targetdomain1.com/some_page.htm',
    onload:     function (responseDetails) {
                    // DO ALL RESPONSE PROCESSING HERE...
                    console.log (
                        "GM_xmlhttpRequest() response is:n",
                        responseDetails.responseText.substring (0, 80) + '...'
                    );
                }
} );

You should also get in the habit of using the @connect directive -- even though it's not strictly required for Greasemonkey on Firefox, yet.

Thursday, September 30, 2021
 
JZ.
answered 2 Months ago
JZ.
83

No, there is no point in wrapping your script code in a jQuery() call, nor in using $(document).ready ().

Greasemonkey automatically fires after both the DOMContentLoaded event (same as the jQuery wrapper or ready event), and after GM's version of jQuery is loaded.

Also note that that wiki page is outdated. GM works fine with jQuery 1.6.2 now.

You didn't show the relevant code, nor link to the target page, but the most-likely reasons the "Greasemonkey code is not executed every time the page is refreshed" are:

  1. There is an error in the GM script.

  2. The target content is loaded separately, via AJAX.
    You can use code in this pattern, to get around that:

    //--- This handles both page-load delays, and AJAX changes.
    setInterval (function() { checkForTweetbox (); }, 500);
    
    function checkForTweetbox () {
        var tweetbox = document.querySelector ('div.tweet-box textarea');
        if (tweetbox) {
            if (! tweetbox.weHaveProcessed) {
                tweetbox.weHaveProcessed    = true;
                alert ('New tweet-box found!');
            }
        }
    }
    
Tuesday, October 5, 2021
 
Rudie
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share