Search code examples
javascriptjquerygreasemonkey

onload function for specific window


I'm a beginner so I know this is probably very basic but I just can't seam to figure it out. I'm writing a greasemonkey script to auto open a few links from a page . What i would like to do is if link1 is open do one thing and if link2 is opened do something different, but since the links are var's im not sure how to call the different functions to the different windows. Here is how im opening the windows

var link1= $(' a:eq(1)').attr('href');
var link2= $(' a:eq(3)').attr('href');
var site1= window.open(link1);
var site2= window.open(link2);

Is there a way to call the different functions by the variable name like $(site1).ready(function() { //do this //}); $(site2).ready(function() { //do something else //});


Solution

  • // ==UserScript==
    // @name         Multiple Sites
    // @version      1
    // @description  Multiple Sites
    // @match        http://stackoverflow.com/questions/28732645/onload-function-for-specific-window
    // @match        http://www.amazon.com/
    // @match        http://www.bing.com/
    // @grant        GM_openInTab
    // @noframes
    // ==/UserScript==
    
    if(location.href === 'http://stackoverflow.com/questions/28732645/onload-function-for-specific-window') {
        var link1 = 'http://www.amazon.com/';
        var link2 = 'http://www.bing.com/';
        //using GM_openInTab as window.open can be blocked by browser pop-up blockers
        GM_openInTab(link1);
        GM_openInTab(link2);
    }
    else if(location.href === 'http://www.amazon.com/') {
        console.log('This is printed in console of Amazon window. ');
        //do this
    }
    else if(location.href === 'http://www.bing.com/') {
        console.log('This is printed in console of Bing window. ');
        //do something else
    }
    

    You may also wish to learn about GM_xmlhttpRequest.