Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 22

Thread: Discussion: Standards in relay over-ride scripts

  1. #1
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Discussion: Standards in relay over-ride scripts

    Relay over-ride scripts end up having naming conflicts with other relay over-ride scripts that function on the same page. I've been considering this, and there is a simple solution which might save you (the script writer) time in the future. It is fairly simple, and would make importing the re-named script into a new "shell" script a lot less time consuming.

    This will be a reference for myself and others to use. Contributions/suggestions are welcomed.

    • [li]avoid global variable declarations where possible.[/li]
      [li]Place all code which does something to the source in a function, and pass the source to the function as a parameter. Return the modified source.[/li]
      [li]Try to use function names that will be unique to your script[/li]
      [li]Place all calls to your created functions inside void main()[/li]
      [li]Avoid top level commands[/li]


    Here is an example. It uses my Link back to manage store at the top of the store log page script as an example. Strangely I wasn't thinking about this when I wrote the script, but it follows the ideas.

    storelog.ash
    PHP Code:
    buffer addtoplink(string source)
    {
    buffer results;
    results.append(source);
    results.replace_string(
      
    "<td style=\"padding: 5px; border: 1px solid blue;\"><center><table>",
      
    "<td style=\"padding: 5px; border: 1px solid blue;\"><center><table><tr><td><center><a href=\"managestore.php\">Back to Your Store</a></center></td></tr><tr><td></td></tr>"
      
    );

    return 
    results;
    }

    void main()
    {
    addtoplink(visit_url()).write();

    Now when another script is written to do something else on the same page, rename the file to storelogtoplink.ash, then write a new storelog.ash file as follows:

    Code:
    import storelogtoplink.ash;
    //import new script here
    
    void main()
    {
    buffer source;
    source.append(visit_url());
    
    //code to call new script function can be here
    source = addtoplink(source);
    //or it can be here
    
    source.write();
    }
    Because void main() is not used in an imported script, we need to do nothing to the original script (unless it conflicts in some way with what the new script is doing). Not having top level commands means that we are less likely to have a command executed before it's time. Avoiding global variables means we won't end up with variable naming conflicts. The use of unique names ahead of time will reduce the chances of a naming conflict in function names. (naming all your functions which make changes to the source "modify_source" is not such a good idea.

    Comments, suggestions, Edits by other moderators/admins, and any improvements to this concept are not only welcomed, but will be enjoyed by me as it will make what I try to do in the future better.

  2. #2
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default A Different Implementation?

    Would Greasemonkey-style annotations be useful?

    In other words, rather than relying on the name of the script, maybe add an additional capability where KoLmafia scans through the initial comment blocks for something along the lines of @include and @exclude to see whether or not the script should be injected, and scans all the .ash scripts sitting in the /relay folder each time it encounters a KoL page. This might slow things down (slightly) because it's no longer a quick lookup, but it may be useful in the short term for testing?

  3. #3
    Member
    Join Date
    Apr 2007
    Location
    Russia
    Posts
    63

    Default Re: Discussion: Standards in relay over-ride scripts

    Why search for scripts, if you can use "all"? Just execute relay/PAGENAME/*.ash.

    Furthermore, mafia can do all the dirty work herself, passing current page buffer to scripts and writing output when all scripts are done.
    My KoLmafia script repository: gitweb, snapshot in tar.gz archive

  4. #4
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default Re: Discussion: Standards in relay over-ride scripts

    The idea would be to make it so that instead of making a set number of scripts work on one page, make it so every script can run on every page.

    Suppose, for example, you wanted to write a simple script which imitated the color KoL images which ran everywhere, rather than strictly on Firefox. You could write hundreds of scripts which all contained the same exact code, or you could have a single script applied globally.

    I didn't think people would use it that way, but it's entirely possible that this "inject in more than one place" idea may be better, as it's far more flexible.

  5. #5
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Re: Discussion: Standards in relay over-ride scripts

    Sorry I didn't get back sooner. I have been offline for a while because my ISP wanted to raise the price of my internet service to a silly level.

    My thoughts are that rather than having Kolmafia search for a script every time it encounters a kol page, Kolmafia should already know if there is one. This would avoid the slower hardware access. I also see a possible problem with the order of script execution. That problem will become more obvious as more relay over-ride scripts are shared. There are several ways to handle both issues, but they all boil down to kolmafia keeping a list.

    • [li]Method 1: change all relay over-ride scripts from .ash to .rly, and have a register method. The register method would contain info about what pages the script should run on. When the script is manually ran, kolmafia would read only the register method and ignore the rest of the script until encountering the page. This would require a file be saved by kolmafia for re-loading at next run, which would contain a list of registered scripts, and how they are registered. That file could then be manually edited by the user if the order the scripts run needs modified or a script needs removed.[/li]

      [li]Method 2: kolmafia could search the relay directory at start-up, and read the info as indicated in Holatuwol's post above, and store the info in memory. Kolmafia would then check it's stored info for a script which should run. This method is less desirable because when adding a new script, it would require a restart. Also, this method leaves the order of execution issue unsolved.[/li]

      [li]Method 3: have a manually created/edited file in the relay folder (call it relay.inf during this post) and when Kolmafia starts, load the file if it exists. This file would tell Kolmafia how to handle relay over-rides. This method would allow the removal of the option to enable Relay over-ride scripts. If the file exists, then Relay over-rides are on, if not then it's off.
      Relay.inf would follow a format similar to this: (horribly similar to windows .inf files)
      • [li]{all}[/li]
        [li]myscript4[/li]
        [li][/li]
        [li]{storelog}[/li]
        [li]myscript1[/li]
        [li]myscript2[/li]
        [li][/li]
        [li]{adventure}[/li]
        [li]myscript1[/li]
        [li]myscript3[/li]


      this would also require a restart.
      [/li]


    I personally prefer method 1, or a variation of it.

    All these methods leave open the question of non-standard kol pages. One of the first things I did when relay over-rides was in it's infancy was to create my own page called BottomMenu.html. It contains some of the links I use the most for faster access. I have considered adding to that page using the newer form of relay over-rides where I can create the links based on variables like my inventory status. That brings to light the question of creating custom java scripts, but that is a whole new topic. I might start a topic later on that explaining in detail.

  6. #6
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default Re: Discussion: Standards in relay over-ride scripts

    If I follow a Greasemonkey implementation model, method 1 = method 2, except method 1 uses register() and method 2 uses script comments.

  7. #7
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Re: Discussion: Standards in relay over-ride scripts

    In general, method 1 = method 2, as you said except that method 1 uses register() and method 2 uses script comments. That is where the big difference is though. The Register method would allow adding a new relay over-ride script without restarting kolmafia. Since kolmafia would only have to load 1 file at startup, the list of registered scripts, kolmafia should start faster using method 1.

    I know a lot of users of Greasemonkey are not aware of this, but if you go to manage user script, and click-hold a script name, and drag it up or down, this changes the order the script appears in the list. It's noted below the list. It does more than that though, it changes the order of execution too. That's the important thing. Here is an example of where that might apply:

    This pseudo code will insert the word hi at the beginning of every table:
    script 1:
    PHP Code:
    document.replacestring"<table>""<table>hi" ); 
    This pseudo code will insert the word bye at the first table row of every table:
    script 2:
    PHP Code:
    document.replacestring"<table><tr>""<table><tr>bye" ); 
    script 1 breaks script 2 if script 1 runs before script 2. If script 2 runs first, it will be fine, and script 1 will not be hurt either.

    Adding a Greasemonkey like interface with the movement feature would work, but I don't feel it appropriate to ask that you do so much extra work adding that part of the interface because I think it is probably going to be more work than is needed. I do think some way of changing the order of execution is needed though.

    The code I used above is probably not using proper methods of changing html files, but it is the easiest way that I have found to achieve the simple things like injecting a link into a page. You will find this method actually applied here.

  8. #8
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default Re: Discussion: Standards in relay over-ride scripts

    I can always implement a 'register' command which does exactly what method 1 does (except in a way that's less cryptic than a new file extension) which avoids the adding scripts weakness you speak of. Using a Greasemonkey storage model avoids the ordering weakness (I need not have the UI -- any script injection which doesn't know in advance what it needs will cache this data in some file).

    The thing is, worrying too much about the whole 'oh, two scripts will break each other' problem feels counterintuitive. Ideally, whenever I'm looking for new ideas for the relay browser, I would implement some of the changes you guys post here in addition to some of my own. Should I be discouraged from making changes to the core because they might break the scripts you guys are writing? I don't think so.

    One of the reasons I think people avoid writing these scripts is that KoLmafia's relay browser HTML is a moving target, which is a good thing for KoLmafia but a bad thing for script writers. You're operating in that environment, so giving you the illusion that you run in a sandbox doesn't make much sense.

  9. #9
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Re: Discussion: Standards in relay over-ride scripts

    [quote author=holatuwol link=topic=1538.msg7522#msg7522 date=1205005015]
    The thing is, worrying too much about the whole 'oh, two scripts will break each other' problem feels counterintuitive. [/quote]

    The very root of this topic...the first post is about making 2 or more scripts work on the same page. In order for 2 or more scripts to work on the same page, they must not break each other. I'm lost ???

    [quote author=holatuwol link=topic=1538.msg7522#msg7522 date=1205005015]
    Ideally, whenever I'm looking for new ideas for the relay browser, I would implement some of the changes you guys post here in addition to some of my own. Should I be discouraged from making changes to the core because they might break the scripts you guys are writing? I don't think so.
    [/quote]

    No, not "I don't think so", just no. Doing so would severely limit your ability to upgrade Kolmafia. That concept would be counterintuitive. That is on the same level as: If Intel refused to make a change to the instruction set in a new model of processor because it would break Windows, that would be counterintuitive. Think of Kolmafia as the processor, and the script as Windows.

    If Microsoft realized that a change to Excel broke Word, and didn't fix it (or couldn't), Microsoft Office's sales would plummet very quickly as people started switching to Corel Office, or Open Office, or some other office suite. Both Excel and Word run in Windows, so now think of Kolmafia as Windows, and the scripts as Excel and Word.

  10. #10
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default Re: Discussion: Standards in relay over-ride scripts

    [quote author=efilnikufecin link=topic=1538.msg7526#msg7526 date=1205042825]
    The very root of this topic...the first post is about making 2 or more scripts work on the same page. In order for 2 or more scripts to work on the same page, they must not break each other. I'm lost
    [/quote]

    In the ideal case, the HTML that KoLmafia presents won't be a static target. Therefore, trying to meticulously plan out a mechanism to protect you from each other when I'm the most likely person to break everything you're writing feels counterintuitive.

Similar Threads

  1. continuationState and relay scripts
    By roippi in forum Scripting Discussion
    Replies: 17
    Last Post: 09-27-2014, 04:37 PM
  2. Relay override scripts have never worked for me. Help?
    By quechanmok in forum Community Support
    Replies: 15
    Last Post: 08-07-2013, 07:49 PM
  3. Relay Scripts Post-SVN
    By bumcheekcity in forum Relay Override Scripts
    Replies: 5
    Last Post: 06-01-2013, 11:27 AM
  4. Work in progress relay over-ride questions
    By efilnikufecin in forum Community Support
    Replies: 0
    Last Post: 06-09-2008, 06:18 AM
  5. Re: Discussion of Standards
    By Presto Ragu in forum Community Support
    Replies: 2
    Last Post: 03-30-2006, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •