GM: More than one in... One?

Discussion in 'Code Snippets and Tutorials' started by Fexxel, Mar 22, 2009.

  1. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    How do I put more than 1 script in a script?
    I am trying to do combind these scripts:
    This one:
    Code (Text):
    1. // ==UserScript==
    2. // @name          Pwning Website
    3. // @namespace     http://www.neofriends.net/
    4. // @description   script to alert "You are on a pwning website" on every page
    5. // @include       http://www.neofriends.net/
    6. // @exclude       http://www.neofriends.net/*  
    7. // ==/UserScript==
    8.  
    9. alert('You are on a pwning website');
    10.  
    and this one:
    Code (Text):
    1. // ==UserScript==
    2. // @name          Pwning Website
    3. // @namespace     http://www.neofriends.net/
    4. // @description   script to alert that you are posting on neopets
    5. // @include       http://www.neofriends.net/posting.php?*
    6. // @exclude       http://www.neofriends.net/  
    7. // ==/UserScript==
    8.  
    9. alert('You are about to post something');
    Is there a command to combind more than one code set?
    Thanks,
    Fexxel
     
  2. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    I don't think so. Why don't you just copy over the code of one script to the other?
     
  3. Freestyle

    Freestyle Level II

    Joined:
    Oct 21, 2007
    Messages:
    235
    Likes Received:
    8
    Easyyyyyyyyyyyyyyyyyyyyy.

    Lemme make an example.
     
  4. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    Code (Text):
    1.  
    2. // ==UserScript==
    3. // @name          Pwning Website
    4. // @namespace     http://www.neofriends.net/
    5. // @description   two in one
    6. // @include       http://www.neofriends.net/*
    7. // ==/UserScript==
    8. if (document.location.href == 'http://www.neofriends.net/'){
    9.   alert('You are on a pwning website');
    10. }
    11. else if (document.location.href.match('posting.php?')){
    12.   alert('You are about to post something');
    13. }
    14.  
    15.  
    16.  
     
  5. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Could I do:
    Code (Text):
    1. // ==UserScript==
    2. // @name          Pwning Website
    3. // @namespace     http://www.neofriends.net/
    4. // @description   two in one
    5. // @include       http://www.neofriends.net/*
    6. // ==/UserScript==
    7. if (document.location.href == 'http://www.neofriends.net/'){
    8.   alert('You are on a pwning website');
    9. }
    10. else if (document.location.href.match('posting.php?')){
    11.   alert('You are about to post something');
    12. }
    13. else if (document.location.href.match('downloads.php)){
    14.  alert('You are in the Downloads Section');
    15. }
    ???
    Thanks for the help guys :)
     
  6. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    Test it yourself and see if it does what you want/expect it to do.

    Leaving it as
    Code (Text):
    1.  match('downloads.php')
    means that it will give the alert on downloads.php as well as downloads.php?d=163
     
  7. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Alright, I tried it and it and it didn't work.
    Here's my updated code:
    Code (Text):
    1.     // ==UserScript==
    2.     // @name          Pwning Website
    3.     // @namespace     http://www.neofriends.net/
    4.     // @description   Testing this for neofriends.net
    5.     // @include       http://www.neofriends.net/*
    6.     // ==/UserScript==
    7.     if (document.location.href == 'http://www.neofriends.net/'){
    8.       alert('You are on a pwning website. It might be too pwnful for you. Are you sure you want to continue?');
    9.     }
    10.     else if (document.location.href == 'http://www.neofriends.net/posting.php?')){
    11.       alert('You are about to post something. You might get flamed.');
    12.     }
    13.     else if (document.location.href == 'http://www.neofriends.net/downloads.php')){
    14.       alert('You are in the Downloads Section')
    Just wondering what's wrong with it. According to US.org all my text is wrong. The whole thing is red. I don't completely understand the match thing. Remember that this is my first day with GM xD
     
  8. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    You can use error console to find errors. (tools > error console) You had an error the first time. you had match('downloads.php)

    it should've been match('downloads.php')

    In your updated code you need to close out the last if statement. Add } to the end of the script.

    match returns true if it finds what's between the parenthesis () in the string. In my example you were trying to find 'downloads.php' in the window's URL
     
  9. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Ahah! Well, no, I don't have an error console. Is there an application I should be using because I'm typing up the scripts manually on userscripts.org xD
    Oh, and should I be using match('xxx') or is the href == thing fine?
     
  10. jazzeh

    jazzeh Level I

    Joined:
    Jan 1, 2008
    Messages:
    144
    Likes Received:
    15
    you can use == if you want it to only alert on a single page. The alert you have for posting might not ever fire because there's no time that you'll post a message and the URL will be 'http://www.neofriends.net/posting.php' exactly.

    You can edit scripts without using userscripts.org. Right click on the greasemonkey head on the status bar then right click on the script to edit it. If nothing happenes then you need to setup you default editor at about:config search for greasemonkey.editor and change the value to your favorite editor. try : C:\Windows\notepad.exe You can also edit your scripts by right clicking the head and then click manage your scripts. Highlight your script and click edit.
     
  11. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    I'm on Mac OS X. :-/ I'd prefer to program in Mac OS X, it doesn't crash as frequently as Vista.
    So is this valid:
    Code (Text):
    1.     // ==UserScript==
    2.     // @name          Pwning Website
    3.     // @namespace     http://www.neofriends.net/
    4.     // @description   Testing this for neofriends.net
    5.     // @include       http://www.neofriends.net/*
    6.     // ==/UserScript==
    7.     if (document.location.href == 'http://www.neofriends.net/'){
    8.       alert('You are on a pwning website. It might be too pwnful for you. Are you sure you want to continue?');
    9.     }
    10.     else if (document.location.href.match('http://www.neofriends.net/posting.php?')){
    11.       alert('You are about to post something. You might get flamed.');
    12.     }
    13.     else if (document.location.href.match('http://www.neofriends.net/downloads.php')){
    14.       alert('You are in the Downloads Section')
    15. }
    And how do I get the last } to be exactly aligned with the other "}"'s?
    So when I do match it's the same as http://www.xxxxx.com/posting.php?* ?