Web Dev Notes

Blog about web development by Robert Dawson

Save to file all links on page

This code below works in Chrome's console (on Mac, but should work on Windows), assuming that jQuery is loaded on the page. Run this script, then right-click and save. I also renamed the resulting file with a .txt extension to see a preview in Finder.

jQuery('*').find('a').each(function(){
  console.log(jQuery(this).attr('href'));
});

Bookmarklet to switch subdomains

If you ever find yourself needing to switch subdomains, such as from beta.mysite.com to www.mysite.com, here is a bookmarklet to make life easier. Because dragging my mouse to the address bar, selecting the entire subdomain, and then tediously typing in a new one is so exhausting!

With this handy bookmarklet, all you have to do is click it and type in the subdomain you want. It's not telepathy, but it's tolerable. To make this bookmarklet, make a new bookmark on any given page and then replace the URL with the following code.

javascript:var env=prompt("Subdomain?","e.g., www");if(env!="e.g., www"){window.location=window.location.href.replace(/http:\/\/(.+?)\.(.+?)\.(.+)/g, "http://"+env+".$2.$3");};

Automator script to copy all open URLs in Safari to clipboard

Problem

You need to send someone (or do anything with) a list of URLs you have open (in Safari or Chrome on Mac).

Solution

Use this Automator workflow script:

on run {input, parameters}
	set _urls to {}
	tell application "Safari"
		repeat with _tab in tabs of window 1
			set end of _urls to URL of _tab
		end repeat
	end tell
	set {_tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
	set the clipboard to _urls as string
	set AppleScript's text item delimiters to _tid
	
	return input
end run

Then, just paste wherever you like!

Greasemonkey script to convert Redmine URLs into actual hyperlinks

I use Redmine and each issue page contains a field identified as "URL(s) to be edited," which contains URLs related to the issue at hand. Unfortunately, these URLs aren't actually linked! They're just text. And I have been tediously copying and pasting each link, one at a time, into a new tab in my browser.

Well, not anymore! Today, to ease my suffering (and to try something new), I created my first Greasemonkey script to convert those text-only URLs into hyperlinks. I hope it's helpful, as this script could be easily modified to work with any text in any HTML element (thanks so much to jQuery).

To use this script, first install a Greasemonkey-compatible extension in your browser. Greasemonkey has traditionally been primarily used in Firefox, but you can also add Greasemonkey support in Chrome (and probably IE, but why even check?). Then, install my script! And modify to suit your own needs.