Dziamid
Dziamid

Reputation: 11571

Load a page and apply javascript in a single bookmark

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!

Upvotes: 1

Views: 1661

Answers (3)

Erik Knepfler
Erik Knepfler

Reputation: 21

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.

Upvotes: 0

David W. Keith
David W. Keith

Reputation: 2254

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.

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

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.

Upvotes: 1

Related Questions