├── Drafts.js ├── Editor.js ├── OmniFocus.js ├── README.md ├── Search.js ├── Simplenote.js ├── drafts-markdown.js ├── pastebot-link.js ├── pastebot-markdown-link.js ├── pastebot-source.js ├── run-in-safari.sh ├── search-site-duckduckgo.js ├── search-site-google.js ├── site-search.js └── swap-search-engine.js /Drafts.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20linkTitle%20=%20encodeURIComponent(document.title);var%20linkLocation%20=%20encodeURIComponent(window.location);var%20newLocation%20=%20%27drafts://x-callback-url/create?text=%27+linkTitle+escape(%22\n%22)+linkLocation;var%20selectedText%20=%20%27%27;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText%20!=%20%27%27)%20{newLocation%20=%20newLocation%20+%20escape(%22\n\n%22)%20+%20selectedText;}window.location%20=%20newLocation; 2 | 3 | // Adds the current web page title, URL, and text selection to Pastebot, which automatically copies it to the clipboard. I.e., this is a shortcut to copy the URL, web page title, and text selection to the clipboard. 4 | 5 | // Note: iOS only. 6 | 7 | // Get the link 8 | var linkTitle = encodeURIComponent(document.title); 9 | var linkLocation = encodeURIComponent(window.location); 10 | var newLocation = 'drafts://x-callback-url/create?text='+linkTitle+escape("\n")+linkLocation; 11 | 12 | // Get the selection 13 | // Note: This method of getting the selected text works on Safari for Mac and on iPad as long as the bookmark is in the bookmarks bar. It does not work on iPhone. 14 | var selectedText = ''; 15 | if (window.getSelection) { 16 | selectedText = window.getSelection(); 17 | } else if (document.getSelection) { 18 | selectedText = document.getSelection(); 19 | } else if (document.selection) { 20 | selectedText = document.selection.createRange().text; 21 | } 22 | // Add the selectedText to the new location 23 | if (selectedText != '') { 24 | newLocation = newLocation + escape("\n\n") + selectedText; 25 | } 26 | 27 | window.location = newLocation; -------------------------------------------------------------------------------- /Editor.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20regularExpression%20=%20/((&|\?)q=)([^&]*)(?!.*&q=)/g;var%20match%20=%20regularExpression.exec(window.location);var%20searchString%20=%20match[3];searchString%20=%20decodeURIComponent(searchString);searchString%20=%20searchString.replace(/\+site:.*$/,%20%27%27);searchString%20=%20searchString.replace(/\%22/g,%20%27%27);searchString%20=%20%22\%22%22%20+%20searchString%20+%20%22\%22%22;siteSearches%20=%20%22+site:nytimes.com+OR+site:newyorker.com%22;searchString%20=%20searchString%20+%20siteSearches;window.location%20=%20%22http://www.google.com/search?q=%22%20+%20searchString; 2 | 3 | // To assist in copy editing, it takes a phrase just searched for in Google and narrows the search to just the websites for The New York Times and The New Yorker. The idea being, if those sites are doing it, then it's probably edited correctly. 4 | 5 | // Usage: 6 | // 1. While authoring a document, you aren't sure if you're punctuating or using a phrase correctly. 7 | // 2. Copy that phrase to the clipboard. 8 | // 3. Perform a google search with that phrase. (You have to actually hit the search button, or otherwise perform the search, before triggering the bookmarklet.) 9 | // 4. Trigger this bookmarklet, your search will be narrowed to just well-edited publications. 10 | // 5. If your usage is in the search results, then it's probably correct. 11 | 12 | // Grab the Search String 13 | var regularExpression = /((&|\?)q=)([^&]*)(?!.*&q=)/g; 14 | // ((&|\?)q=) Match the first &q= 15 | // [^&]* Till an & 16 | // (?!.*&q=) Make sure this is the last &q= 17 | var match = regularExpression.exec(window.location); 18 | // The third match is the search string 19 | var searchString = match[3]; 20 | 21 | // Decode the URL to simplify matching 22 | searchString = decodeURIComponent(searchString); 23 | 24 | // Remove everything from "site:" to the end, just to prevent any funny business 25 | // for hitting the bookmarklet multiple times. 26 | searchString = searchString.replace(/\+site:.*$/, ''); 27 | 28 | // Remove any quotes in the current search string " 29 | searchString = searchString.replace(/\"/g, ''); 30 | 31 | // Wrap the searchString in quotes 32 | searchString = "\"" + searchString + "\""; 33 | 34 | // Add the site searches 35 | siteSearches = "+site:nytimes.com+OR+site:newyorker.com"; 36 | searchString = searchString + siteSearches; 37 | 38 | window.location = "http://www.google.com/search?q=" + searchString; -------------------------------------------------------------------------------- /OmniFocus.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20linkTitle%20=%20encodeURIComponent(document.title);var%20linkLocation%20=%20encodeURIComponent(window.location);var%20newLocation%20=%20%27omnifocus:///add?name=%27+linkTitle+%27¬e=%27+linkLocation;var%20selectedText%20=%20%27%27;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText%20!=%20%27%27)%20{newLocation%20=%20newLocation%20+%20escape(%22\n\n%22)%20+%20encodeURIComponent(selectedText);}window.location%20=%20newLocation; 2 | 3 | // OmniFocus bookmarklet that adds the current web page as a new action in OmniFocus. This bookmarklet extends the official OmniFocus bookmarklet by adding the currently selected text to the action's note. 4 | 5 | // Get the link 6 | var linkTitle = encodeURIComponent(document.title); 7 | var linkLocation = encodeURIComponent(window.location); 8 | var newLocation = 'omnifocus:///add?name='+linkTitle+'¬e='+linkLocation; 9 | 10 | // Get the selection 11 | // Note: This method of getting the selected text works on Safari for Mac and on iPad as long as the bookmark is in the bookmarks bar. It does not work on iPhone. 12 | var selectedText = ''; 13 | if (window.getSelection) { 14 | selectedText = window.getSelection(); 15 | } else if (document.getSelection) { 16 | selectedText = document.getSelection(); 17 | } else if (document.selection) { 18 | selectedText = document.selection.createRange().text; 19 | } 20 | // Add the selectedText to the new location 21 | if (selectedText != '') { 22 | newLocation = newLocation + escape("\n\n") + encodeURIComponent(selectedText); 23 | } 24 | 25 | window.location = newLocation; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bookmarklets 2 | 3 | A collection of useful bookmarklets. 4 | 5 | ### Drafts 6 | 7 | Opens Drafts with the current web page title, URL, and selected text as a new entry. Requires [Drafts](http://agiletortoise.com/drafts/), iOS only. 8 | 9 | ### Drafts Markdown 10 | 11 | Opens Drafts with the current web page title, URL, and selected text as Markdown as a new entry. Requires [Drafts](http://agiletortoise.com/drafts/), iOS only. 12 | 13 | ### Editor 14 | 15 | Takes the current Google search and narrows it to just _The New York Times_ and _The New Yorker_ websites, useful for copy editing. 16 | 17 | ### OmniFocus 18 | 19 | Creates a new action in OmniFocus using the web page title, URL, and selected text. Requires [OmniFocus](http://www.omnigroup.com/products/omnifocus/). 20 | 21 | ### Pastebot Link 22 | 23 | Opens Pastebot with the current web page title, URL, and selected text as a new Pastebot entry, which also copies it to the clipboard. I.e., this acts as a shortcut to copy the web page title, URL, and selected text to the clipboard all at once. Requires [Pastebot](http://tapbots.com/software/pastebot/), iOS only. 24 | 25 | ### Pastebot Markdown Link 26 | 27 | Same as above but makes a Markdown Link to the current web page (and ignores the selected text). Requires [Pastebot](http://tapbots.com/software/pastebot/), iOS only. 28 | 29 | ### Pastebot Source 30 | 31 | Opens Pastebot with the current web page's source, which copies it to the clipboard. I.e., this is a shortcut to copy the source of the current web page to the clipboard. Requires [Pastebot](http://tapbots.com/software/pastebot/), iOS only. 32 | 33 | ### Simplenote 34 | 35 | Creates a new Simplenote note with the current web page title, URL, and selected text. Requires [Simplenote](http://itunes.apple.com/us/app/simplenote/id289429962?mt=8), iOS only. 36 | 37 | ### Search Site 38 | 39 | Loads a Google site search of the base URL of the currently viewed site. I.e., this is a quick way to start searching the site you are currently viewing. 40 | 41 | ### Site Search 42 | 43 | Uses the last search term of a Google search to create a Google site search, i.e., `drexciya allmusic` becomes `drexciya site:allmusic.com`. This is as close as I could get to a [Smart Bookmarks](http://en.wikipedia.org/wiki/Smart_Bookmarks) replacement that works on iOS. 44 | 45 | ## Supplements 46 | 47 | ### Run in Safari 48 | 49 | To ease development of bookmarklets, this shell script takes JavaScript from stdin and runs it in the frontmost Safari window. It can be used as a TextMate command or Automator service that runs JavaScript as a bookmarklet. 50 | 51 | It does not work correctly in BBEdit. BBEdit's text filter feature replaces the current document with the output of the script; if this script runs correctly, it outputs nothing, therefore it will delete the contents of the document. So alternatively, there's [a gist of a pure AppleScript version for BBEdit](https://gist.github.com/3026027) that works perfectly. 52 | 53 | ## Notes 54 | 55 | 1. Further explanations of each bookmarklet are in each bookmarklet's comments. 56 | 57 | 2. These bookmarklets are built using [Daring Fireball: JavaScript Bookmarklet Builder](http://daringfireball.net/2007/03/javascript_bookmarklet_builder). To install a bookmarklet, cut and paste its first line (minus the `//`). To edit a bookmarklet, you'll need to visit the _JavaScript Bookmarklet Builder_ page and understand it. 58 | -------------------------------------------------------------------------------- /Search.js: -------------------------------------------------------------------------------- 1 | var initialLocation = window.location.href; 2 | var duckDuckGoPrefix = "https://duckduckgo.com/"; 3 | var googlePrefix = "https://www.google.com/search"; 4 | var googleImageParameter = "&tbm=isch"; 5 | var duckImageParameter = "&ia=images&iax=images"; 6 | var findString; 7 | var replaceString; 8 | var secondFindString; 9 | var secondReplaceString; 10 | if (initialLocation.lastIndexOf(duckDuckGoPrefix, 0) === 0) { 11 | findString = duckDuckGoPrefix; 12 | replaceString = googlePrefix; 13 | secondFindString = duckImageParameter; 14 | secondReplaceString = googleImageParameter; 15 | } else if (initialLocation.lastIndexOf(googlePrefix, 0) === 0) { 16 | findString = googlePrefix; 17 | replaceString = duckDuckGoPrefix; 18 | secondFindString = googleImageParameter; 19 | secondReplaceString = duckImageParameter; 20 | } else { 21 | var regularExpression = /\/([^\s\/]+)/g; 22 | var match = regularExpression.exec(window.location); 23 | var searchString = match[1]; 24 | window.location = 25 | googlePrefix + "?q=site:" + searchString + encodeURIComponent(" "); 26 | } 27 | if (findString) { 28 | var newLocation = initialLocation 29 | .replace(findString, replaceString) 30 | .replace(secondFindString, secondReplaceString); 31 | window.location = newLocation; 32 | } 33 | -------------------------------------------------------------------------------- /Simplenote.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20linkTitle%20=%20encodeURIComponent(document.title);var%20linkLocation%20=%20encodeURIComponent(window.location);var%20newLocation%20=%20%27simplenote://new?content=%27+linkTitle+escape(%22\n%22)+linkLocation;var%20selectedText%20=%20%27%27;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText%20!=%20%27%27)%20{newLocation%20=%20newLocation%20+%20escape(%22\n\n%22)%20+%20selectedText;}window.location%20=%20newLocation; 2 | 3 | // Adds the current web page title, url, and text selection to a new note in Simplenote. 4 | 5 | // Note: iOS only. 6 | 7 | // Get the link 8 | var linkTitle = encodeURIComponent(document.title); 9 | var linkLocation = encodeURIComponent(window.location); 10 | var newLocation = 'simplenote://new?content='+linkTitle+escape("\n")+linkLocation; 11 | 12 | // Get the selection 13 | // Note: This method of getting the selected text works on Safari for Mac and on iPad as long as the bookmark is in the bookmarks bar. It does not work on iPhone. 14 | var selectedText = ''; 15 | if (window.getSelection) { 16 | selectedText = window.getSelection(); 17 | } else if (document.getSelection) { 18 | selectedText = document.getSelection(); 19 | } else if (document.selection) { 20 | selectedText = document.selection.createRange().text; 21 | } 22 | // Add the selectedText to the new location 23 | if (selectedText != '') { 24 | newLocation = newLocation + escape("\n\n") + selectedText; 25 | } 26 | 27 | window.location = newLocation; -------------------------------------------------------------------------------- /drafts-markdown.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20linkTitle%20=%20encodeURIComponent(%22[%22%20+%20document.title%20+%20%22]%22);var%20linkLocation%20=%20encodeURIComponent(%22(%22%20+%20window.location%20+%20%22)%22);var%20newLocation%20=%20%27drafts://x-callback-url/create?text=%27+linkTitle+linkLocation;var%20selectedText%20=%20%27%27;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText%20!=%20%27%27)%20{selectedText%20=%20selectedText.toString();selectedText%20=%20selectedText.replace(/^/gm,%20%22>%20%22);newLocation%20=%20newLocation%20+%20encodeURIComponent(%22:%22)%20+%20escape(%22\n\n%22)%20+%20encodeURIComponent(selectedText);}window.location%20=%20newLocation; 2 | 3 | // Sends a markdown URL to the current web page to Drafts 4 | 5 | // Note: iOS only. 6 | 7 | // Get the link 8 | var linkTitle = encodeURIComponent("[" + document.title + "]"); 9 | var linkLocation = encodeURIComponent("(" + window.location + ")"); 10 | var newLocation = 'drafts://x-callback-url/create?text='+linkTitle+linkLocation; 11 | 12 | // Get the selection 13 | // Note: This method of getting the selected text works on Safari for Mac and on iPad as long as the bookmark is in the bookmarks bar. It does not work on iPhone. 14 | var selectedText = ''; 15 | if (window.getSelection) { 16 | selectedText = window.getSelection(); 17 | } else if (document.getSelection) { 18 | selectedText = document.getSelection(); 19 | } else if (document.selection) { 20 | selectedText = document.selection.createRange().text; 21 | } 22 | // Add the selectedText to the new location 23 | if (selectedText != '') { 24 | selectedText = selectedText.toString(); 25 | selectedText = selectedText.replace(/^/gm, "> "); 26 | newLocation = newLocation + encodeURIComponent(":") + escape("\n\n") + encodeURIComponent(selectedText); 27 | } 28 | 29 | window.location = newLocation; 30 | -------------------------------------------------------------------------------- /pastebot-link.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20linkTitle%20=%20encodeURIComponent(document.title);var%20linkLocation%20=%20encodeURIComponent(window.location);var%20newLocation%20=%20%27pastebot://%27+linkTitle+%22\n%22+linkLocation;var%20selectedText%20=%20%27%27;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText%20!=%20%27%27)%20{newLocation%20=%20newLocation%20+%20escape(%22\n\n%22)%20+%20selectedText;}window.location%20=%20newLocation; 2 | 3 | // Adds the current web page title, URL, and text selection to Pastebot, which automatically copies it to the clipboard. I.e., this is a shortcut to copy the URL, web page title, and text selection to the clipboard. 4 | 5 | // Note: iOS only. 6 | 7 | // Get the link 8 | var linkTitle = encodeURIComponent(document.title); 9 | var linkLocation = encodeURIComponent(window.location); 10 | var newLocation = 'pastebot://'+linkTitle+"\n"+linkLocation; 11 | 12 | // Get the selection 13 | // Note: This method of getting the selected text works on Safari for Mac and on iPad as long as the bookmark is in the bookmarks bar. It does not work on iPhone. 14 | var selectedText = ''; 15 | if (window.getSelection) { 16 | selectedText = window.getSelection(); 17 | } else if (document.getSelection) { 18 | selectedText = document.getSelection(); 19 | } else if (document.selection) { 20 | selectedText = document.selection.createRange().text; 21 | } 22 | // Add the selectedText to the new location 23 | if (selectedText != '') { 24 | newLocation = newLocation + escape("\n\n") + selectedText; 25 | } 26 | 27 | window.location = newLocation; -------------------------------------------------------------------------------- /pastebot-markdown-link.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20linkTitle%20=%20encodeURIComponent(%22[%22%20+%20document.title%20+%20%22]%22);var%20linkLocation%20=%20encodeURIComponent(%22(%22%20+%20window.location%20+%20%22)%22);var%20newLocation%20=%20%27pastebot://%27+linkTitle+linkLocation;var%20selectedText%20=%20%27%27;if%20(window.getSelection)%20{selectedText%20=%20window.getSelection();}%20else%20if%20(document.getSelection)%20{selectedText%20=%20document.getSelection();}%20else%20if%20(document.selection)%20{selectedText%20=%20document.selection.createRange().text;}if%20(selectedText%20!=%20%27%27)%20{selectedText%20=%20selectedText.toString();selectedText%20=%20selectedText.replace(/^/gm,%20%22>%20%22);newLocation%20=%20newLocation%20+%20encodeURIComponent(%22:%22)%20+%20escape(%22\n\n%22)%20+%20encodeURIComponent(selectedText);}window.location%20=%20newLocation; 2 | 3 | // Sends a markdown URL to the current web page to Pastebot, which automatically copies it to the clipboard. I.e., this is a shortcut to copy a markdown link to the current web page to the clipboard. 4 | 5 | // Note: iOS only. 6 | 7 | // Get the link 8 | var linkTitle = encodeURIComponent("[" + document.title + "]"); 9 | var linkLocation = encodeURIComponent("(" + window.location + ")"); 10 | var newLocation = 'pastebot://'+linkTitle+linkLocation; 11 | 12 | // Get the selection 13 | // Note: This method of getting the selected text works on Safari for Mac and on iPad as long as the bookmark is in the bookmarks bar. It does not work on iPhone. 14 | var selectedText = ''; 15 | if (window.getSelection) { 16 | selectedText = window.getSelection(); 17 | } else if (document.getSelection) { 18 | selectedText = document.getSelection(); 19 | } else if (document.selection) { 20 | selectedText = document.selection.createRange().text; 21 | } 22 | // Add the selectedText to the new location 23 | if (selectedText != '') { 24 | selectedText = selectedText.toString(); 25 | selectedText = selectedText.replace(/^/gm, "> "); 26 | newLocation = newLocation + encodeURIComponent(":") + escape("\n\n") + encodeURIComponent(selectedText); 27 | } 28 | 29 | window.location = newLocation; -------------------------------------------------------------------------------- /pastebot-source.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20source%20=%20encodeURIComponent(document.documentElement.innerHTML);var%20newLocation%20=%20%27pastebot://%27+source;window.location%20=%20newLocation; 2 | 3 | // Adds the current web page title, URL, and text selection to Pastebot, which automatically copies it to the clipboard. I.e., this is a shortcut to copy the URL, web page title, and text selection to the clipboard. 4 | 5 | // Note: iOS only. 6 | 7 | // Get the link 8 | var source = encodeURIComponent(document.documentElement.innerHTML); 9 | var newLocation = 'pastebot://'+source; 10 | window.location = newLocation; -------------------------------------------------------------------------------- /run-in-safari.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Takes JavaScript from stdin as input and runs it in in the frontmost Safari window. Useful for testing bookmarklets. It can easily be adapted to a TextMate bundle or Automator service. 4 | 5 | # Usage example from the command line: 6 | # ./run-in-safari.sh < Editor.js 7 | 8 | tempfile=$(mktemp "${TMPDIR:-/tmp}/tmux-paths.XXXX") 9 | cat > "$tempfile" 10 | 11 | osascript <<-APPLESCRIPT 12 | try 13 | set theBookmarkletFile to "$tempfile" 14 | set theJavaScript to read theBookmarkletFile 15 | tell application "Safari" 16 | activate 17 | do JavaScript theJavaScript in document 1 18 | end tell 19 | return -- Suppress superfluous AppleScript output 20 | on error errMsg number errNum 21 | display dialog "AppleScript encountered an error." & ¬ 22 | " Error Message: " & errMsg & " Number " & errNum 23 | end try 24 | APPLESCRIPT 25 | 26 | rm $tempfile -------------------------------------------------------------------------------- /search-site-duckduckgo.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20regularExpression%20=%20/\/([^\s\/]+)/g;var%20match%20=%20regularExpression.exec(window.location);var%20searchString%20=%20match[1];window.location%20=%20%22http://www.duckduckgo.com/?q=site:%22%20+%20searchString%20+%20encodeURIComponent(%22%20%22); 2 | 3 | // Loads a Google site search of the base URL of the currently viewed site. I.e., this is a quick way to start searching the site you are currently viewing. 4 | 5 | var regularExpression = /\/([^\s\/]+)/g; 6 | // Matches the first instance of a slash followed by matching until reaching a character that is whitespace or a slash 7 | 8 | var match = regularExpression.exec(window.location); 9 | var searchString = match[1]; 10 | 11 | window.location = "http://www.duckduckgo.com/?q=site:" + searchString + encodeURIComponent("%20"); -------------------------------------------------------------------------------- /search-site-google.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20regularExpression%20=%20/\/([^\s\/]+)/g;var%20match%20=%20regularExpression.exec(window.location);var%20searchString%20=%20match[1];window.location%20=%20%22http://www.google.com/search?q=site:%22%20+%20searchString%20+%20encodeURIComponent(%22%20%22); 2 | 3 | // Loads a Google site search of the base URL of the currently viewed site. I.e., this is a quick way to start searching the site you are currently viewing. 4 | 5 | var regularExpression = /\/([^\s\/]+)/g; 6 | // Matches the first instance of a slash followed by matching until reaching a character that is whitespace or a slash 7 | 8 | var match = regularExpression.exec(window.location); 9 | var searchString = match[1]; 10 | 11 | window.location = "http://www.google.com/search?q=site:" + searchString + encodeURIComponent("%20"); -------------------------------------------------------------------------------- /site-search.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20regularExpression%20=%20/((&|\?)q=)([^&]*)(?!.*&q=)/g;var%20match%20=%20regularExpression.exec(window.location);var%20searchString%20=%20match[3];var%20searchArray%20=%20searchString.split(%22+%22);var%20siteSearchTerm%20=%20searchArray.pop();var%20siteSearchTermArray%20=%20siteSearchTerm.split(%22.%22);if%20(siteSearchTermArray.length%20==%201%20||%20siteSearchTermArray.pop().length%20>%203)%20{siteSearchTerm%20=%20siteSearchTerm%20+%20%22.com%22;}if%20(siteSearchTerm.indexOf(%22site%22)%20==%20-1)%20{var%20siteSearchTerm%20=%20%22site:%22%20+%20siteSearchTerm;}searchArray.push(siteSearchTerm);window.location%20=%20%22http://www.google.com/search?q=%22+searchArray.join(%22+%22); 2 | 3 | // Takes the last term of a Google search and makes a Google site search out of it, for example: "bookmarklet news.ycombinator" becomes "bookmarklet site:news.ycombinator.com". Basically allows a shorthand for creating site searches. 4 | 5 | // Usage: 6 | // 1. Construct a search in Google and perform it, e.g., search for "bookmarklet news.ycombinator". (You have to actually hit the search button, or otherwise perform the search, before triggering the bookmarklet.) 7 | // 2. Trigger the bookmarklet. 8 | // 3. A new search will be performed using the last search term to construct a site search. E.g., "bookmarklet news.ycombinator" becomes "bookmarklet site:news.ycombinator.com". 9 | 10 | // It's sort of smart about how it constructs a search. For example: 11 | // 1. "bookmarklet news.ycombinator" turns into "bookmarklet site:news.ycombinator.com". I.e., the ".com" will be added if it's missing. 12 | // 2. "bookmarklet news.ycombinator.com" turns into "bookmarklet site:news.ycombinator.com". I.e., ".com" won't be added if it's already there. 13 | // 3. "bookmarklet daringfirball.net" turns into "bookmarklet site:daringfirball.net" I.e., ".com" won't be added if an alternative top-level domain is already specified. 14 | 15 | var regularExpression = /((&|\?)q=)([^&]*)(?!.*&q=)/g; 16 | // ((&|\?)q=) Match the first &q= 17 | // [^&]* Till an & 18 | // (?!.*&q=) Make sure this is the last &q= 19 | 20 | var match = regularExpression.exec(window.location); 21 | var searchString = match[3]; 22 | var searchArray = searchString.split("+"); 23 | var siteSearchTerm = searchArray.pop(); 24 | 25 | // Add ".com" as the last section if there is no extension already 26 | var siteSearchTermArray = siteSearchTerm.split("."); 27 | if (siteSearchTermArray.length == 1 || siteSearchTermArray.pop().length > 3) { 28 | siteSearchTerm = siteSearchTerm + ".com"; 29 | } 30 | // Add "site:" if it's missing 31 | if (siteSearchTerm.indexOf("site") == -1) { 32 | var siteSearchTerm = "site:" + siteSearchTerm; 33 | } 34 | 35 | // Add the first search term back to the array and construct the query 36 | searchArray.push(siteSearchTerm); 37 | window.location = "http://www.google.com/search?q="+searchArray.join("+"); -------------------------------------------------------------------------------- /swap-search-engine.js: -------------------------------------------------------------------------------- 1 | // javascript:var%20initialLocation%20=%20window.location.href;var%20duckDuckGoPrefix%20=%20%22https://duckduckgo.com/?q=%22;var%20googlePrefix%20=%20%22https://www.google.com/search?client=safari&rls=en&q=%22;if%20(initialLocation.lastIndexOf(duckDuckGoPrefix,%200)%20===%200)%20{findString%20=%20duckDuckGoPrefix;replaceString%20=%20googlePrefix;}%20else%20{findString%20=%20googlePrefix;replaceString%20=%20duckDuckGoPrefix;}newLocation%20=%20initialLocation.replace(findString,%20replaceString);window.location%20=%20newLocation; 2 | 3 | // Swaps a DuckDuckGo search to Google and vice versa. 4 | 5 | var initialLocation = window.location.href; 6 | var duckDuckGoPrefix = "https://duckduckgo.com/?q="; 7 | var googlePrefix = "https://www.google.com/search?client=safari&rls=en&q="; 8 | if (initialLocation.lastIndexOf(duckDuckGoPrefix, 0) === 0) { 9 | findString = duckDuckGoPrefix; 10 | replaceString = googlePrefix; 11 | } else { 12 | findString = googlePrefix; 13 | replaceString = duckDuckGoPrefix; 14 | } 15 | newLocation = initialLocation.replace(findString, replaceString); 16 | window.location = newLocation; --------------------------------------------------------------------------------