10 votes

Total noob looking for (hopefully) simple greasemonkey script

I have knowledge of the basic concepts of programming in general and html and some very basic knowledge of javascript, but this specific task is proving a little beyond me. I'm actually using tampermonkey, in case that matters.

www.bricklink.com is a site to buy Lego from private sellers. By default, when looking at a shop's listing of items, it shows 25 per page. I would like to automatically switch to 100 per page every time.

Here's a randomly selected store page (no affiliation) at the default 25 per page:

https://store.bricklink.com/TheBricky#/shop?o={"itemType":"P","catID":"18","showHomeItems":0}

Now, same page set to display 100 per page. Note how "pgSize" is added to the url but doesn't appear by default:

https://store.bricklink.com/TheBricky#/shop?o={"pgSize":100,"itemType":"P","catID":"18","showHomeItems":0}

What I would like is for pgSize to be set to 100 only IF

"shop" appears in the url

AND

"pgSize" does not appear in the url OR "pgSize" does appear in the url but does not equal 100.

Since Bricklink remembers pgSize per shop page per session, once pgSize is set to 100 for a particular shop greasemonkey doesn't need to do anything. Intercepting the url before the page loads would be nice but unnecessary since loading is fast and I'm not worried about bandwidth.

I tried making this but wasn't sure how to input what I'm trying to test for in the url. Of course now that I've thought about it some more it seems the task is more probably difficult than I thought it would be at first. Any help would be appreciated.

EDIT: This comment below seems to be working, although the way Bricklink makes their urls feels funky at times.

9 comments

  1. [4]
    Emerald_Knight
    Link
    That is a really bizarre URL structure. If it helps, start with retrieving window.location.hash. You could then grab everything after ?o= and parse the JSON to transform it into an object. If that...

    That is a really bizarre URL structure.

    If it helps, start with retrieving window.location.hash. You could then grab everything after ?o= and parse the JSON to transform it into an object. If that object doesn't contain the pgSize property or that property has a value that isn't 100, then you can add the property with value 100. After that, you can convert the object back into JSON and replace the URL hash with one that contains the new JSON. For the record, the "hash" is the # and everything that comes after it. So you would e.g. do window.location.hash = '#/shop?o=...';.

    I'm not really able to write up some code at the moment, but that should get you going in the right direction.

    9 votes
    1. [3]
      chips
      Link Parent
      (function() { 'use strict'; function parseArgs(argsString) { const args = {}; const argsParts = argsString.split('&'); argsParts.forEach((part) => { const [name, value] = part.split('=');...
      (function() {
          'use strict';
      
          function parseArgs(argsString) {
              const args = {};
              const argsParts = argsString.split('&');
              argsParts.forEach((part) => {
                  const [name, value] = part.split('=');
                  args[decodeURIComponent(name)] = decodeURIComponent(value);
              });
              return args;
          }
      
          function generateArgs(args) {
              const argsArray = Object.entries(args).map(([name, value]) => {
                  return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
              });
              const argsString = argsArray.join('&');
              return argsString;
          }
      
          function setPGSize() {
              const hash = window.location.hash;
              const [hashPath, hashArgsString] = hash.split('?');
              if (hashPath.indexOf("shop") !== -1) {
                  const hashArgs = parseArgs(hashArgsString);
                  if (hashArgs.hasOwnProperty('o')) {
                      const o = JSON.parse(hashArgs.o);
                      if (o.pgSize !== 100) {
                          o.pgSize = 100;
                          hashArgs.o = JSON.stringify(o);
                          const newHashArgsString = generateArgs(hashArgs);
                          const newHashString = [hashPath, newHashArgsString].join('?');
                          window.location.hash = newHashString;
                      }
                  }
              }
          }
      
          setPGSize()
      })();
      
      5 votes
      1. Emerald_Knight
        Link Parent
        I'm actually surprised that this works, given that it generates JSON that should be invalid. Nice work!

        I'm actually surprised that this works, given that it generates JSON that should be invalid. Nice work!

        1 vote
      2. zptc
        Link Parent
        This seems to do the trick. Thanks very much!

        This seems to do the trick. Thanks very much!

        1 vote
  2. MacDolanFarms
    (edited )
    Link
    Simplest way would probably be this, which seems to work: const url = window.location.href; if (url.indexOf("shop") !== -1 && url.indexOf("pgSize") === -1) { window.location = url.replace('{',...

    Simplest way would probably be this, which seems to work:

    const url = window.location.href;
    
    if (url.indexOf("shop") !== -1 && url.indexOf("pgSize") === -1) {
    	window.location = url.replace('{', '{"pgSize":100,');
    }
    

    I didn't make it check if pgSize is set but not 100 but that should be enough to get you started.

    6 votes
  3. [3]
    poboxy
    Link
    I am no programmer but if you create an account in the site and go to your search setting you can set the search result to show up to 500 items per page.

    I am no programmer but if you create an account in the site and go to your search setting you can set the search result to show up to 500 items per page.

    3 votes
    1. [2]
      zptc
      Link Parent
      Thanks, but that doesn't affect shop pages.

      Thanks, but that doesn't affect shop pages.

  4. what
    Link
    To give a quick answer, you may want to look into Regex, it could probably solve your URL checking problem.

    To give a quick answer, you may want to look into Regex, it could probably solve your URL checking problem.

    2 votes