简体   繁体   English

JavaScript Bookmarklet; 单击按钮,重新加载页面,然后打开页面

[英]JavaScript Bookmarklet; click button, reload page, then open page

I am trying to create a bookmarklet that follows this process: 我正在尝试创建一个遵循此过程的bookmarklet:

  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? 我需要添加某种wait()函数吗? If I do add a wait method, will it kill the JavaScript and not open the new window? 如果我确实添加了一个wait方法,它会杀掉JavaScript并且不会打开新窗口吗? I'm fairly new to JavaScript, so I'm not familiar with these types of mechanics. 我对JavaScript很新,所以我不熟悉这些类型的机制。

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. Edit2:更新了该过程。

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. A.)将加载页面的进程更改为使用AJAX。

B.) Change the process that loads the page to instead open a new window, and then use JavaScript to manipulate the new window. B.)更改加载页面的过程而不是打开一个新窗口,然后使用JavaScript来操作新窗口。

C.) Before triggering the page load, open a new child window and insert code into it. C.)在触发页面加载之前,打开一个新的子窗口并在其中插入代码。 The code in that child window can then monitor its parent and take actions on the parent even after the parent has reloaded. 然后,该子窗口中的代码可以监视其父级,并在父级重新加载后对父级执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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