Load a page and apply javascript in a single bookmark

后端 未结 3 647
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-12 03:40

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

相关标签:
3条回答
  • 2020-12-12 03:43

    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.

    0 讨论(0)
  • 2020-12-12 03:53

    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.

    0 讨论(0)
  • 2020-12-12 03:54

    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.

    0 讨论(0)
提交回复
热议问题