Javascript 书签转到 URL 并执行

发布于 2024-11-30 13:36:51 字数 280 浏览 2 评论 0原文

我有一个像这样的 javascript,使用了 ia bookmarklet:

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

问题是 javascript 一旦到达 URL 就会停止。 如何让它进入页面并执行脚本?

任何帮助将不胜感激。

I have a javascript like this, used i a 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.
How to make it go to a page and execute a script?

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洒一地阳光 2024-12-07 13:36:51

我不推荐浏览器扩展 - 它们允许不受信任的代码在受信任的代码上下文中运行。如果您有一个浏览器扩展程序可以加载页面并自动输入您的用户 ID 和密码,那么什么可以阻止它将这些凭据发布到恶意站点呢?只有你,而且只有你仔细阅读了源代码。

为了解决上述问题,我在浏览器的收藏夹栏中添加了书签,需要单击两次:
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(); 
 }

祝你好运!

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? 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

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!

-黛色若梦 2024-12-07 13:36:51

不幸的是,您的书签将无法使用。更改 window.location 会导致新页面的加载 - 如果没有先显示文档,则无法在该页面上执行任何 Javascript。

所需的操作可以通过浏览器扩展来实现,例如 Greasemonkey 脚本,该扩展在页面加载时在页面上执行指定的 Javascript。

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.

The desired action can be achieved by browser extensions, e.g. Greasemonkey script, which execute specified Javascript on the page upon page load.

我恋#小黄人 2024-12-07 13:36:51

如果您控制正在加载的页面,我会考虑将该数据作为片段传递并从页面处理该数据。像 window.location="URL#username=...;password=..." 这样的东西,而不是解析页面中片段的数据并用它做任何您需要的事情。但是,在这种特定情况下,这不是一个好主意,因为密码将保存在浏览历史记录中。

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. However, in this specific case its not a good idea as the password will be saved in the browsing history.

度的依靠╰つ 2024-12-07 13:36:51

如果您想从与目标位置相同的域(并且具有相同的方案)加载的页面执行小书签,您可以通过将其加载到 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%>';

请注意,为了易读,多行字符串使用了反引号。需要 onload = null 来防止无限循环,因为 onload 将在为位置分配 JavaScript 代码后触发。

(仅在 Chrome 中测试。)

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.

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.

(Only tested in Chrome.)

疑心病 2024-12-07 13:36:51

尝试使用 window.onload 事件包装您的代码。

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

由于上述解决方案不起作用,您仍然应该能够编写一个简单的浏览器扩展来解决您的问题。这很简单:选择您使用的浏览器并按照文档进行操作。这是唯一的方法。

Try wrapping your code with window.onload event.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文