user948557
user948557

Reputation: 43

Opera userscript is loading too often

I made user script. I test this on Opera 11.62. I have a code:

(
    function() {
        var headID = document.getElementsByTagName("head")[0];         
        var newScript = document.createElement('script');
        newScript.type = 'text/javascript';
        newScript.id = 'myjQuery';
        newScript.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
        headID.appendChild(newScript);

        newScript.addEventListener('load', function (e) {
            jQuery (
                function($) {
                    alert('loaded');
                }
            );          
        },false);

        newScript.removeEventListener('load');
    }
)();

My problem is that it's showing the alert message too many times, each time with different domain in the alert title.

I know it's Facebook's and advertisement windows' fault.
Can I load a userscript after all scripts on a site are loaded?

Upvotes: 1

Views: 217

Answers (1)

Brock Adams
Brock Adams

Reputation: 93493

It seems the problem is that Opera is firing the script for each iframe. Adding this code to the top of your script should clear that up:

if (window.top != window.self)  //-- Don't run on frames or iframes.
    return;


Also, it's not clear that you are setting the script's targets the way Opera expects.

Unless you are running the script in Greasemonkey mode, be sure your code is wrapped by domain/URL sensing like so:

if (location.hostname.indexOf('example.com') != -1) {
    // YOUR CODE HERE
}

See "Loading scripts" in the Opera manual.

Upvotes: 5

Related Questions