- By instantiating the variables on one line, we save some length by avoiding repeated
var r = …
,var l = …
, etc. - This regular expression (or 'regex') is a logical test which can check if any given text is an ISBN. The regex is stored in the variable
r
. It works by checking for:(?:97[89])?
— a prefix ((?:___)
) of:97
— the characters '97', followed by[89]
— a character that is in the set of '8' and '9' (ISBN-13s start with '978' or '979'),?
zero or one times; followed by
[-\s]?
— a hyphen or space (\s
) character zero or one times; followed by[0-9]{1}
— a character within the range of 0–9, exactly once (ISBNs can have space or hyphen separators in them); followed by[-\s]?
— a hyphen or space (\s
) character zero or one times; followed by[0-9]{3}
— a character within the range of 0–9, exactly three times; followed by[-\s]?
— a hyphen or space (\s
) character zero or one times; followed by[0-9]{3}
— a character within the range of 0–9, exactly three times; followed by[-\s]?
— a hyphen or space (\s
) character zero or one times; followed by[0-9]{5}
— a character within the range of 0–9, exactly two times; followed by[-\s]?
— a hyphen or space (\s
) character zero or one times; followed by[0-9X]{3}
— a character within the range of 0–9 and 'X', exactly one time (the ISBN 'check digit').
- This line gets the current URL (
window.location.href
) and the current selection (window.getSelection().toString()
), then matches it against the regex (.match(r)
), and stores an array of the results in the variablel
. - This line gets the current selection (
window.getSelection().toString()
), then matches it against the regex (.match(r)
), and stores an array of the results in the variables
. - Thse lines set the variable
a
to the current active element, then if that element has avalue
property (ie. is an<input>
or<textarea>
), sets the variablev
to an array of matches tor
found in text that is selected within that element. This fixes non-WebKit browsers not included<input>
s inwindow.getSelection().toString()
. - This line sets the variable
i
to be a blank string — this will be the default path added to the Booko URL. - These lines work by each checking if the variable (
l
,s
, orv
) exists and is set, then if it is, setsi
to be the first result ([0]
) stored in that variable. By testings
andv
afterl
, any ISBN which has been highlighted in the text on the page will override an ISBN in the URL. - This changes the current URL to Booko's URL, plus the extracted ISBN (if any) stripped of hyphens and spaces, navigating the broswer there. If there's no ISBN, nothing will be added to the Booko URL, and the browser will navigate to Booko's home page.
- Wrapping the function in parentheses like this:
(function(){…})();
, is called an immediately-invoked function expression, and simultaneously defines and runs the function.
Last active
May 26, 2022 10:49
-
-
Save ngsctt/ce3167517692d55627384b4178cdfe73 to your computer and use it in GitHub Desktop.
How to make a bookmarklet that finds the book in a product page on Booko.com.au
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var r, l, s, a, v, i; // (1) | |
r = /(?:97[89])?[-\s]?[0-9]{1}[-\s]?[0-9]{3}[-\s]?[0-9]{3}[-\s]?[0-9]{2}[-\s]?[0-9X]{1/; // (2) | |
l = window.location.href.match(r); // (3) | |
s = window.getSelection().toString().match(r); // (4) | |
a = document.activeElement; | |
if (a.value) { v = a.value.slice(a.selectionStart, a.selectionEnd).match(r) } // (5) | |
i = ""; // (6) | |
if (l) { i = l[0] } | |
if (s) { i = s[0] } | |
if (v) { i = v[0] } // (7) | |
location.href="https://booko.com.au/" + i.replace(/[-\s]/g, ""); // (8) | |
})(); // (9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment