正在加载页面并在其上执行 JS...从 JS 书签?

发布于 2024-08-21 13:46:54 字数 141 浏览 3 评论 0原文

本质上,我试图

  1. 导航到一个网页,
  2. 等待该网页加载,
  3. 执行该页面上的 JS 函数/警报/任何内容

并从单个书签中 。这可能吗?我似乎无法为自己工作,但这可能是因为我个人的失败。

Essentially I'm trying to

  1. navigate to a webpage
  2. wait for that webpage to load
  3. execute a JS function/alert/whatever on that page

all from a single bookmarklet. Is this possible? I can't seem to get onload to work for me, but that may be because of my own personal failings here.

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

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

发布评论

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

评论(4

奈何桥上唱咆哮 2024-08-28 13:46:54

我发现不需要 Greasemonkey 或类似的东西就能做到这一点的最简单方法是编写 JS,以便它检查它是否在适当的页面上,如果不在则转到那里。如果它在页面上,那么它会执行 JS/alert/whatever。您必须使用该小书签两次,但您只需要一个小书签,并且用户进行点击/无论他或她自己做什么可能仍然更快/更容易。所以代码看起来像这样:

if(this.document.location.href != "[url]") { //Are we on the page yet? 
  this.document.location.href = "[url]"; // If not, go there
}
else {
  if (document.readyState === "complete") { //Wait for the page to finish loading
   // DO STUFF
  }
}

The simplest way I found to do this without needing Greasemonkey or something similar is to write your JS so that it checks to see if it is on the appropriate page, and goes there if it isn't. If it is on the page, then it executes the JS/alert/whatever. You have to use the bookmarklet twice, but you just need one bookmarklet, and it may still be quicker/easier the user doing the clicking/whatevering him or herself. So the code would look like this:

if(this.document.location.href != "[url]") { //Are we on the page yet? 
  this.document.location.href = "[url]"; // If not, go there
}
else {
  if (document.readyState === "complete") { //Wait for the page to finish loading
   // DO STUFF
  }
}
阪姬 2024-08-28 13:46:54

您想要安装适用于 Firefox 的 Greasemonkey 扩展程序。 (或 gm4ie 对于 IE,或 greasemetal 适用于 Chrome (PersonalizedWeb 在 Chrome 中也能以更简单的方式工作),greasekit 适用于 Safari,或 user.js for Opera)

Greasemonkey 可以让您做到这一点...在每次页面加载时自动运行脚本(您可以选择它加载的页面/站点)

否则,您将需要在每个页面加载时单击小书签才能运行脚本。

You want to install the Greasemonkey extension for Firefox. (or gm4ie for IE, or greasemetal for Chrome (PersonalizedWeb also works in a much simpler way for Chrome), greasekit for Safari, or user.js for Opera)

Greasemonkey lets you do exactly this... run a script automatically on every page load (you can choose what pages/sites it loads on)

Otherwise you will need to click your bookmarklet on every page load in order to run your script.

情归归情 2024-08-28 13:46:54

鉴于没有更好的解决方案,我想我应该放弃 Opera 本身支持在每个页面加载时运行的用户脚本。从那里,您可以让脚本检查当前 url,并在适当的页面上运行。

请参阅此处获取文档

Given there's no better solution, I thought I'd toss out that Opera natively supports user scripts to run on every page load. From there, you could have the script check the current url, and run if on appropriate page.

See here for documentation

雨后彩虹 2024-08-28 13:46:54

另一种选择是调用window.open(...),并使用窗口对象来操作窗口。也可以通过这种方式导航多个页面。

Another option is to call window.open(...), and use the window object to manipulate the window. It is also possible to navigate multiple pages this way.

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