2

This is an example what I would like to do. Whenever we are on target url i.e. stackoverflow to appear a sticky footer on the bottom with buttons. One of the buttons to type something in search, submit the form. After this it waits for page to load and do something the page that just loaded, i.e. click on the first link.

I found out this is not possible by just running a click after the submit because the frames of the page change or something like that, how would it be possible to do this with tampermonkey.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    $('body').append('<div id="test"><p><button onclick="myFunction()">Click me</button></p></div>');
    $('body').append(
        `<script type="text/javascript">
             function myFunction() {
                 // page 1
                 document.querySelector('#search > div > input').value="tampermonkey";
                 document.forms[0].submit();

                 // page 2 (DOEST WORK)
                 document.querySelector('#question-summary-29592068 > div.summary > div.result-link > h3 > a');
             }
         </script>`
    );

    $('#test').css({'position': 'fixed',
                   'left': '0',
                   'bottom': '0',
                   'width': '100%',
                   'background-color': 'red',
                   'color': 'white',
                   'text-align': 'center',
                   'z-index':'1'
                  });

})();
skillsboy
  • 319
  • 6
  • 14
  • 1
    the problem is that you submit and expect the code to continue running - you'll need to run different code depending on which page you are on – Jaromanda X Mar 10 '20 at 23:19
  • Exactly, I found somewhere that I could use post message or something like that, but I don't really understand how. – skillsboy Mar 10 '20 at 23:40
  • 1
    postmessage works across iframes ... not across pages that are not in the browser - `document.forms[0].submit();` replaces current page with new page ... you can't communicate with the old page from the new page, since the old page is no longer in the browser - as I said, you'll need to run different code depending on the page – Jaromanda X Mar 10 '20 at 23:59
  • Ahh ok, so the only option here with tampermonkey is for whatever the url the submit gets create a separate script for that? – skillsboy Mar 11 '20 at 00:20
  • 2
    can be the same monkey script - just need some logic in your script – Jaromanda X Mar 11 '20 at 00:23
  • Ah that's nice, i will explore that. Thank you! – skillsboy Mar 11 '20 at 00:29
  • Related: https://stackoverflow.com/a/61052335 – CertainPerformance Nov 19 '22 at 17:15

1 Answers1

1

One reason you script might not be working is because at the top of the script where it says

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/
// @grant        none
// ==/UserScript==


The // @match only matches on the stackoverflow.com homepage which doesn't include the search page so the @match should be // @match https://stackoverflow.com/* which does match the homepage and search page. You should also make it so the "Click Me!" button only shows on the homepage by using a if statement. I also completely solved your problem if you want that.

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/*
// @grant        none
// ==/UserScript==

window.onload = function() {
    'use strict';
    if (location.href == "https://stackoverflow.com/search?q=tampermonkey") {
        alert(document.querySelector('#question-summary-29592068 > div.summary > div.result-link > h3 > a'));
    }else {
        $('body').append('<div id="test"><p><button onclick="myFunction()">Click me</button></p></div>');
        $('body').append(
            `<script type="text/javascript">
                 function myFunction() {
                     // page 1
                     document.querySelector('#search > div > input').value="tampermonkey";
                     document.forms[0].submit();
                 }
            </script>`
        );

        $('#test').css({
            'position': 'fixed',
            'left': '0',
            'bottom': '0',
            'width': '100%',
            'background-color': 'red',
            'color': 'white',
            'text-align': 'center',
            'z-index':'1'
        });
    }

};
Mitigy
  • 74
  • 2
  • 11