简体   繁体   中英

JavaScript Bookmarklet; click button, reload page, then open page

I am trying to create a bookmarklet that follows this process:

  1. From any page, click the bookmarklet
  2. load a specific page.
  3. Trigger an input button which causes page to reload
  4. Once reloaded; open a new page; in this case a social media page.

This is what I've written so far:

if (!($ = window.jQuery)) { 
// Inject jQuery to make life easier  
    script = document.createElement( 'script' );  
    script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';  
    script.onload=SocialCredentials;  
    document.body.appendChild(script);  
}  
else {  
    SocialCredentials();  
}  

function SocialCredentials(){
    $('input:submit').trigger('click'); 
    window.open('http://www.facebook.com', '_blank');
}

The button submit click causes the page to process and clear the credentials of the user so they can visit social sites.

The above code works, but I want to make sure the page finishes loading before opening a new social media page.

Do I need to add some kind of wait() function? If I do add a wait method, will it kill the JavaScript and not open the new window? I'm fairly new to JavaScript, so I'm not familiar with these types of mechanics.

Edit:I should note that I don't have access or control to the page this bookmarklet will work on so I can't alter it's flow. What I'm trying to do is more of a favor for another department as a quick fix until they can make changes on their end.

Edit2: Updated the process.

If a page reloads, any code currently running on that page, including code from a bookmarklet, is ended and removed. Traditionally bookmarklet code ceases to work after a page load and user clicks it again.

There are three workarounds that I know of.

A.) Change the process that loads the page to instead use AJAX.

B.) Change the process that loads the page to instead open a new window, and then use JavaScript to manipulate the new window.

C.) Before triggering the page load, open a new child window and insert code into it. The code in that child window can then monitor its parent and take actions on the parent even after the parent has reloaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM