1

I'm trying to write a greasemonkey script that will automatically perform the Steam store age check before allowing you to view mature game content.

My problem is that after the form is submitted, the website automatically redirects to a new page, and the rest of the greasemonkey script doesn't get a chance to execute:

// ==UserScript==
// @name        agecheck
// @include     http://store.steampowered.com/agecheck/*
// @version     1.17
// @modified    11/23/2012
// @grant       GM_xmlhttpRequest
// ==/UserScript==

//Mini script for doing steam age checks automatically (used when scraping steam sites)
if (/agecheck/i.test (location.pathname) ) {
    var ageForm = document.querySelector ("#agegate_box form");
    ageForm.querySelector ("[name='ageDay']").value     = 18;
    ageForm.querySelector ("[name='ageMonth']").value   = 'August';
    ageForm.querySelector ("[name='ageYear']").value    = 1987;
    ageForm.submit();
    setTimeout( function(){ window.close(); }, 10000); //Never gets to run
}

To verify the code is valid, if I simply comment out the ageForm.submit(); statement, the tab closes after 10 seconds as desired.

How can I get the window to close after submitting the form?

Alain
  • 24,704
  • 19
  • 103
  • 170

2 Answers2

1

You can create an invisible iframe and set form's target attribute to point to that iframe. Thus the page location wont change. An example is in this question.

If you want a pure JS solution, you can check this question, as it explains how to add a callback to submit() method.

Community
  • 1
  • 1
1

I've solved the problem using the following code which loads the result of the form submission in a hidden frame rather than allowing the form to change the current window location (which was halting the greasemonkey script):

if (/agecheck/i.test (location.pathname) ) {
    var ageForm = document.querySelector ("#agegate_box form");
    ageForm.querySelector ("[name='ageDay']").value     = 18;
    ageForm.querySelector ("[name='ageMonth']").value   = 'August';
    ageForm.querySelector ("[name='ageYear']").value    = 1987;

    var newFrame = document.createElement('frame');
    newFrame.name = "PostResult"
    document.body.appendChild(newFrame);
    ageForm.target = newFrame.name;

    ageForm.submit();
    setTimeout( function(){ window.close(); }, 10000);
}

Thanks to G. Kayaalp for the tip.

Alain
  • 24,704
  • 19
  • 103
  • 170