加载页面并在单个书签中应用 javascript

发布于 2024-09-10 15:58:39 字数 414 浏览 5 评论 0原文

我有一个书签,可以打开我的 Google 日历页面 (http://www.google.com/calendar/ renderOnline) 和一个在其上应用一些 javascript 的书签:

javascript:document.getElementById('gadgetcell').setAttribute('style','width:300px');document.getElementsByClassName('sn-frame') [0].setAttribute('样式','宽度:300px');

有没有办法将这些组合成一个书签,这样我就不必一直点击两次?

谢谢你!

I have a bookmark that opens my a google calendar page (http://www.google.com/calendar/renderOnline) and a bookmarklet that applies some javascript on it:

javascript:document.getElementById('gadgetcell').setAttribute('style','width:300px');document.getElementsByClassName('sn-frame')[0].setAttribute('style','width:300px');

Is there a way to combine these into a single bookmarklet so that i don't have to click twice all the time?

Thank you!

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

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

发布评论

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

评论(3

嘿哥们儿 2024-09-17 15:58:39

不可以。小书签在原始页面的上下文中运行,因此它无法更改页面并继续运行。如果您总是想运行该代码,您可能会发现 GreaseMonkey 很有帮助在页面上。或者,时尚可让您将用户样式应用到页面。

No. A bookmarklet runs in the context of the original page, so it can't change pages and keep running. You may find GreaseMonkey helpful if you always want to run that code on the page. Or, Stylish lets you apply user styles to pages.

金兰素衣 2024-09-17 15:58:39

您可以使用扩展来获得相同的行为。

例如,在 Safari 中,您将创建一个启动 URL 的按钮和一个运行小书签的注入脚本。 GreaseMonkey 和许多其他扩展框架可以做类似的事情。

You could use an extension to get the same behavior.

For example in Safari you would create a button that launches the URL and an injected script that runs your bookmarklet. GreaseMonkey and many other extensions frameworks can do similar things.

番薯 2024-09-17 15:58:39

我想到使用小书签来做到这一点:

javascript:location.href='http://google.com';setTimeout(function(){alert('hi');},2000);

setTimeout 函数可以是任何东西,并且由于 2 秒计时器,页面将有时间加载并且可以针对它执行任意代码。但这没有用。无论我似乎尝试在 setTimeout 中调用什么函数,它都永远不会执行,或者可能在加载新页面或类似情况时被破坏的其他空间中执行。看起来,只要 location.href 没有更改,setTimeout 代码就可以作为书签正常工作。

不过,我想知道是否更好的方法是将您想要的页面进行 AJAX 加载到当前空间,然后尝试执行某些操作。如果我有时间尝试,我会在这里更新。

I had the thought to use a bookmarklet to do this:

javascript:location.href='http://google.com';setTimeout(function(){alert('hi');},2000);

The setTimeout function could be anything, and due to the 2 second timer, the page would have time to load and the arbitrary code would execute against it. But it didn't work. No matter what function I seem to try to call in the setTimeout, it just never executes, or perhaps it executes in some other space that was being destroyed as the new page loaded or something like that. That setTimeout code works fine as a bookmarklet as long as there's no location.href change, it seems.

However I wonder if perhaps a better approach would be to do an AJAX load of the page you want into the current space, and then try to execute something. If I get around to trying that I'll update here.

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