简体   繁体   English

Javascript bookmarklet转到URL并执行

[英]Javascript bookmarklet go to URL and execute

I have a javascript like this, used ia bookmarklet: 我有这样的javascript,用过ia bookmarklet:

window.location="URL"
document.getElementById('username-id').value = 'User'
document.getElementById('pwd-id').value = 'pass'
doLogin();

The problem is that javascript stops as soon as it hits the URL. 问题是javascript一旦到达URL就会停止。 How to make it go to a page and execute a script? 如何让它去一个页面并执行一个脚本?

Any help would be greatly appreciated. 任何帮助将不胜感激。

I don't recommend browser extensions - they allow untrusted code to run in the context of trusted code. 我不建议使用浏览器扩展 - 它们允许不受信任的代码在受信任的代码上下文中运行。 If you have a browser extension that loads a page and automatically enters your userID and password, what stops it from posting those credentials to a malicious site? 如果您有一个浏览器扩展程序加载页面并自动输入您的用户ID和密码,是什么阻止它将这些凭据发布到恶意网站? Only you, and only if you've carefully read the source code. 只有你,只有你仔细阅读了源代码。

To solve the problem above I've added bookmarklets to the favorites bar in my browser that require two clicks: 1. Click to navigate to the location 2. Click to fill the form 为了解决上面的问题,我在我的浏览器的收藏夹栏中添加了书签,需要两次点击:1。点击导航到位置2.点击填写表格

javascript: 
 var L='http://mysite.com/';
 if(location!=L)location=L;
 else{
  document.getElementById('username-id').value='User'; 
  document.getElementById('pwd-id').value = 'pass';
  document.close();
  doLogin(); 
 }

Good Luck! 祝好运!

Unfortunately your bookmarklet will not work. 不幸的是,您的书签不起作用。 Change of window.location results loading of the new page - you cannot execute any Javascript on that page without having the document present first. 更改window.location结果加载新页面 - 如果没有首先显示文档,则无法在该页面上执行任何Javascript。

The desired action can be achieved by browser extensions, eg Greasemonkey script, which execute specified Javascript on the page upon page load. 可以通过浏览器扩展来实现所需的操作,例如Greasemonkey脚本,它在页面加载时在页面上执行指定的Javascript。

If you control the page you're loading, I would consider passing that data as the fragment and handling that from the page. 如果您控制正在加载的页面,我会考虑将该数据作为片段传递并从页面处理。 Something like window.location="URL#username=...;password=..." , than parsing the data from the fragment in the page and doing whatever you need with it. window.location="URL#username=...;password=..." ,比从页面中的片段解析数据并用它做任何你需要的东西。 However, in this specific case its not a good idea as the password will be saved in the browsing history. 但是,在这种特定情况下,由于密码将保存在浏览历史记录中,因此不是一个好主意。

If you want to execute the bookmarklet from a page loaded on the same domain (and with the same scheme) as your target location, you can do it (sort of) by loading it into an iframe. 如果要从与目标位置相同的域(并使用相同的方案)加载的页面执行书签,可以通过将其加载到iframe中来完成(排序)。

javascript:
document.body.innerHTML = '<iframe src="http://example.com/page2.html" onload="this.contentWindow.location.assign(`javascript:
    document.body.style.textTransform = \'uppercase\'; /* put code here */
void(0);`); this.onload = null;" width=100% height=100%>';

Note the use of back ticks for a multi-line string for legibility. 请注意,对于多行字符串,请使用后退标记以提高易读性。 The onload = null is required to prevent an infinite loop, as onload will fire after the location is assigned javascript code. onload = null是防止无限循环所必需的,因为onload将在分配位置javascript代码后触发。

(Only tested in Chrome.) (仅在Chrome中测试过。)

Try wrapping your code with window.onload event. 尝试使用window.onload事件包装代码。

window.location = "URL"; window.onload = function() {
document.getElementById('username-id').value = 'User'
document.getElementById('pwd-id').value = 'pass'
doLogin();
}

As the solution above doesn't work, you still should be able to write a simple browser extension to solve your problem. 由于上面的解决方案不起作用,您仍然应该能够编写一个简单的浏览器扩展来解决您的问题。 It's easy: choose the browser you use and follow the documentation. 这很简单:选择您使用的浏览器并按照文档进行操作。 This is only way to do that. 这是唯一的方法。

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

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