加载页面并在单个书签中应用 javascript
我有一个书签,可以打开我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可以。小书签在原始页面的上下文中运行,因此它无法更改页面并继续运行。如果您总是想运行该代码,您可能会发现 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.
您可以使用扩展来获得相同的行为。
例如,在 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.
我想到使用小书签来做到这一点:
setTimeout 函数可以是任何东西,并且由于 2 秒计时器,页面将有时间加载并且可以针对它执行任意代码。但这没有用。无论我似乎尝试在 setTimeout 中调用什么函数,它都永远不会执行,或者可能在加载新页面或类似情况时被破坏的其他空间中执行。看起来,只要 location.href 没有更改,setTimeout 代码就可以作为书签正常工作。
不过,我想知道是否更好的方法是将您想要的页面进行 AJAX 加载到当前空间,然后尝试执行某些操作。如果我有时间尝试,我会在这里更新。
I had the thought to use a bookmarklet to do this:
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.