1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is there a way to keep running Javascript console's instructions after page reloads?

Discussion in 'HTML & JavaScript' started by Ludwig von Drake, Feb 20, 2015.

  1. Ludwig von Drake

    Ludwig von Drake Newbie

    Joined:
    Jun 19, 2014
    Messages:
    4
    Likes Received:
    0
    Hi everyone. I'm new to this, so sorry if this question could seem trivial.
    Let's suppose using browser console to run this loop on a random tab:
    Code:
    var i = 1;
    function myLoop () {           
       setTimeout(function () {    
          location.reload()      
          i++;                     
          if (i < 10) {            
             myLoop();             
          }                        
       }, 3000)
    }
    
    
    myLoop();
    After first reload, console instructions get nulled, and the loop can't continue. This should be normal.
    Now, as title says, I was just wondering if there is a way to execute externally a script by console, and keep it running when page changes - maybe after a form is filled and submitted. I have taken a look for this on Stackoverflow, and it doesn't seem possible; one interesting solution I have found was about including an iframe into a local file, but it isn't clean and could be inapplicable, as long as many sites deny being loaded in iframes.

    Maybe it's just my approach to be wrong, and I'd like to have a confirmation.
    Thank you in advance
     
  2. m4dm4n

    m4dm4n Regular Member

    Joined:
    Sep 15, 2010
    Messages:
    223
    Likes Received:
    92
    Occupation:
    /dev/full
    Location:
    /dev/urandom
    The way i usually do this on chrome is to make a user script that injects that code into the page.

    I don't know any way except plugins/extensions to do this.
     
  3. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    137
    1. Add breakpoint
    2. Reload page
    3. Insert code
    4. Resume

    Above steps are meant for debugging. If you want to use the console for
    automation purposes, then they won't work.
     
  4. Ludwig von Drake

    Ludwig von Drake Newbie

    Joined:
    Jun 19, 2014
    Messages:
    4
    Likes Received:
    0
    Thank you guys, then it was my approach to the console to be wrong.
    At the end I decided to create a Chrome extension and it seems to work efficiently.

    Starting with this assumption, those who would be interested in doing that specific loop have to check manifest's permissions and also replace location.reload() with chrome.tabs.reload() in this way:
    Code:
    var i = 1;                     
    
    
    function myLoop () {           
       setTimeout(function () {    
          chrome.tabs.reload()
          i++;                     
          if (i < 10) {            
             myLoop();             
          }                        
       }, 3000)
    }
    
    
    myLoop();