Skip to content

Commit ea28f87

Browse files
committed
this explains in detail each line of the code of the final bookmarklet
1 parent d3650f1 commit ea28f87

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

find_MailTo_emails_explained.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// find all emails bookmarklet explained (if mailto: hyperlinks)
2+
3+
/* This expanded version explains the code that comprises the FindAllEmailsOnPage
4+
bookmarklet linked in 3rd paragraph of www.recruiting-online.com/bookmarklets.html
5+
which you can drag into your bookmarks/favorites bar */
6+
7+
/* Everything inside these slash-asterisk pairs is a comment in JavaScript and is
8+
ignored by the computer, but it's helpful to us humans in order to figure out what
9+
was intended by the programmer, so comment generously. If your comment is not more
10+
than one line long, you can use a leading double slash and nothing afterwards. Both
11+
comment types are used below. See http://javascript.crockford.com/code.html for
12+
more tips around recommended syntax in JavaScript */
13+
14+
/* normally, a javascript file ends in filetype .js so you don't have to tell the
15+
computer it's JavaScript, but because you'll place the code in a bookmarklet stored
16+
in your web browser, we must begin the code with javascript: and remove all the
17+
spaces in the code. Spaces, tabs, etc., are ignored in JavaScript but do help
18+
readability so we keep them in .js file versions */
19+
20+
DL = document.links;
21+
22+
/* above code line sets variable DL to equal all hyperlinks on the current webpage
23+
(called "document" in JavaScript), and end your statement with a semicolon */
24+
25+
WN = open('','Z6','width=800,height=400,scrollbars,resizable,menubar');
26+
27+
/* unpacking the above line:
28+
- first we create a variable WN that will open a new window
29+
- inside the parentheses are various parameters, the first being URL of page to
30+
open in new window, but by making this blank (2 apostrophes and nothing between
31+
is an empty string), a new window with about:blank is opened
32+
- next parameter is name of the window in case we want to reference it later;
33+
we'll just call it Z6
34+
- next parameter is specs, each separated by a comma. Like most open parameters,
35+
these are optional, but a few are worth having for convenience, such as width
36+
and height in pixels, whether you want the new window to have scrollbars, be
37+
resizable and have a menu bar
38+
- for details on all parameters, see www.w3schools.com/jsref/met_win_open.asp
39+
*/
40+
41+
for(JK=0; JK < DL.length; JK++) {
42+
43+
/* for loops in JavaScript start w/format: (initialization; condition; update).
44+
45+
Note the use of a semicolon to separate statements, which is only obligatory when
46+
you have 2+ statements on the same line. However, when we remove all the spaces
47+
and line breaks for the bookmarklet, that will cause things to be on the same
48+
line, so it is good practice to end each statement with a semicolon.
49+
50+
Blocks of code are surrounded with curly brackets. If you only have one statement
51+
inside a block, you don't need a semicolon. Never put a semicolon after a closing
52+
curly bracket except for assignment statements. For a great explanation on all
53+
this, see www.codecademy.com/en/forum_questions/507f6dd09266b70200000d7e
54+
55+
So, in the "for" line of code above:
56+
- our new variable JK starts off equalling zero,
57+
- the loop will run as many times as JK is less than the total # of links on page,
58+
- variable increments by 1 each time it loops (what double plus sign does; for
59+
more, see https://docs.microsoft.com/en-us/scripting/javascript/reference/increment-and-decrement-operators-javascript
60+
- code line ends with a left curly bracket, indicating the start of a code block
61+
associated with this loop */
62+
63+
if (DL[JK].protocol == 'mailto:')
64+
65+
/* Indenting code lines by 4 spaces inside your for loop is a convention for
66+
readability, but not required. Above code says: if whatever document link # you're
67+
up to has the protocol (type) mailto: (email address), then do the following */
68+
69+
{rr = DL[JK].toString() ;
70+
71+
/* another left curly bracket indicates the start of a code block associated with
72+
this "if" statement, followed by
73+
a new variable rr storing the value in the found mailto: link as a string.
74+
75+
FYI, if you used DL[JK].toString().link(DL[JK]) instead of above code, you'd get
76+
the full hyperlink starting with <a href=" ending with ">whatever</a> whereas
77+
DL[lK].toString() just returns what's between the quotation marks being hyper-
78+
linked. This is advantageous in situations such as where the page displays the
79+
hyperlinked words Email me, but the href is mailto:something@whatever.com --
80+
this script grabs the actual email address rather than the words "Email me". */
81+
82+
WN.document.write(rr.substring(7, rr.length) + '<br>' ;)
83+
84+
/* let's unpack this one:
85+
- WN.document.write says that we will write what follows to the new window, which
86+
will be wrapped in parentheses
87+
- take the portion (substring) of variable rr starting at character #7, running
88+
through the end/length of rr, which is because you'll recall what's inside the
89+
quotation marks of a hyperlink is a string starting with mailto: followed
90+
immediately by the email address, so this omits those first 7 chars and we're
91+
left with just the email!
92+
- plus we add a line return after each email address using +'<br>' (<br> is
93+
HTML's standard tag for a line break) so the final output is easy to read, then
94+
- end-of-statement semicolon, right parentheses to indicate end of what's being
95+
written to the new window */
96+
97+
}}
98+
99+
/* the first curly bracket ends the "if" loop block (which was nested inside the
100+
"for" loop), and the second curly bracket ends the "for" loop */
101+
102+
// We're done!
103+
/* Once you remove the comments, spaces and line breaks so it looks like the
104+
version at (feel free to copy)
105+
https://github.com/gutmach/SourceConExtras/blob/master/find_MailTo_emails.js
106+
Then just right click/edit any favorite/bookmark in your browser and replace the
107+
URL with that, and save it. Now you can click it to run it on whatever webpage
108+
you're on! We call it a bookmarklet, but it's just a bookmark containing
109+
JavaScript squished together. */

0 commit comments

Comments
 (0)