jnaskali / Auto-Claim Itch.io Bundles

Script throws error because jquery library is not referenced. Fixed by adding @require to jquery CDN:

// ==UserScript==
// @name        Auto-Claim Itch.io Bundles
// @author      jnaskali
// @copyright   2020, Juhani Naskali (www.naskali.fi)
// @license     MIT
// @version     1.0
// @namespace   https://www.naskali.fi
// @downloadURL https://openuserjs.org/install/jnaskali/Auto-Claim_Itch.io_Bundles
//
// @match       https://itch.io/bundle/download/*
// @grant       none
//
// @require     https://code.jquery.com/jquery-3.5.1.min.js
//
// @description Automatically clicks all claim links on itch.io bundle download pages. Especially useful to (semi)automatically claim the over 1,500 games in Itch.io Bundle for Racial Justice and Equality. Just add the userscript and visit all download pages to claim the items to your library.
// ==/UserScript==

$(document).ready(function() {
  var clicked = 0;
  $('button[value="claim"').each(function() {
    $(this).parent().attr('target','_blank'); // change claim links to open in new tab(s)
    $(this).click();
    clicked++;
  });
  console.log("Auto-claimed " + clicked + " buttons.");
});

(Also added $(document).ready(function() {...});)

Big thanks for pointing this out and for the DM conversation on Twitter! While I ran this succesfully with ViolentMonkey, it seems Greasemonkey needs to be told explicitly to use unsafeWindow's jQuery. I added the necessary line to the script.

I think userscripts have a @run-at default of document-end, so wrapping it in a ready function doesn't seem necessary, but someone please let me know if something like this is best-practice.

@jnaskali

Re:

I think userscripts have a @run-at default of document-end, so wrapping it in a ready function doesn't seem necessary

GM's @run-at currently states:

The default if no value is provided. The script will run after the main page is loaded, but before other resources (images, style sheets, etc.) have loaded.

jQuery's $(document).ready currently states:

Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

... and also states ...

Code included inside $( window ).on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready.

So if you need complete page load the latter jQuery reference is what to use... otherwise the former and/or GM's @run-at should be okay if the DOM is all you are looking for.

But... Note some types of CSS can add content on the fly with a style sheet. I don't see you using a MutationObserver so it's probably okay for now.

May want to have the noConflict option put in mentioned here for .user.js engines that may need that.

... it seems Greasemonkey needs to be told explicitly to use unsafeWindow's jQuery.

  1. Depends on the GM version and Firefox version. Sandboxing comes and goes (both are facing long term growing pains from Moz and changes) which is why one should always use an IIFE for best practices on ones code.

  2. Sites Cross Origin policy could have changed too, along with Firefox version, to prevent certain things from being shared/utilized.