Skip to content

Commit 1a92afe

Browse files
committed
Merge pull request #2 from ahbeng/ivle-bookmarklet
Support group pages, bigger image and restoring original src; add bookmarklet html
2 parents 04d212e + 0302850 commit 1a92afe

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

IVLE photos/bookmarklet.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>IVLE Photos Bookmarklet</title>
6+
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
7+
</head>
8+
<body>
9+
<div class="container">
10+
<h1>IVLE Photos Bookmarklet</h1>
11+
<p class="lead">This bookmarklet loads student photos in an
12+
<a href="https://ivle.nus.edu.sg/">IVLE</a>
13+
module's Class Roster page or under its Groups page's tabs
14+
(Lecture/Tutorial/Lab Groups).</p>
15+
16+
<a class="btn btn-warning btn-lg" href="javascript:(function%20()%20{var%20calculateNUSMatricNumber%20=%20function%20(id)%20{var%20matches%20=%20id.toUpperCase().match(/^A\d{7}|U\d{6,7}/);if%20(matches)%20{var%20match%20=%20matches[0];if%20(match[0]%20===%20%27U%27%20&&%20match.length%20===%208)%20{match%20=%20match.slice(0,%203)%20+%20match.slice(4);}var%20weights%20=%20{U:%20[0,%201,%203,%201,%202,%207],A:%20[1,%201,%201,%201,%201,%201]}[match[0]];for%20(var%20i%20=%200,%20sum%20=%200,%20digits%20=%20match.slice(-6);%20i%20<%206;%20i++)%20{sum%20+=%20weights[i]%20*%20digits[i];}return%20match%20+%20%27YXWURNMLJHEAB%27[sum%20%25%2013];}};var%20PHOTO_URL%20=%20%27https://mysoc.nus.edu.sg/mysoc/images/stdphoto.php?matric=%27;var%20TYPES%20=%20[%27U%27,%20%27P%27,%20%27X%27];var%20swapImage%20=%20function%20(img)%20{if%20(!img.alt%20||%20img.alt.indexOf(%27Student%20Photograph%27)%20===%20-1)%20{return;}var%20id%20=%20img.id%20||%20img.parentNode.parentNode.nextSibling.innerHTML%20||%20img.parentNode.parentNode.parentNode.nextSibling.innerHTML;var%20matricNumber%20=%20calculateNUSMatricNumber(id);var%20originalSrc%20=%20img.src;var%20photoUrlPrefix%20=%20PHOTO_URL%20+%20matricNumber%20+%20%27&type=%27;var%20typeIndex%20=%200;img.width%20=%20170;img.height%20=%20227;img.onerror%20=%20function%20()%20{img.src%20=%20typeIndex%20<%203%20?%20photoUrlPrefix%20+%20TYPES[typeIndex++]%20:%20originalSrc;};img.onerror.call();};var%20imgs%20=%20document.getElementsByTagName(%27img%27);for%20(var%20i%20=%200;%20i%20<%20imgs.length;%20i++)%20{swapImage(imgs[i]);}})();">
17+
IVLE Photos
18+
</a>
19+
← Drag this button to your Bookmarks Bar.
20+
</div>
21+
</body>
22+
</html>

IVLE photos/bookmarklet.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

IVLE photos/classmate-photos.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,27 @@
3535
};
3636

3737
var PHOTO_URL = 'https://mysoc.nus.edu.sg/mysoc/images/stdphoto.php?matric=';
38-
var NOPHOTO_URL = 'https://ivle.nus.edu.sg/images/nophoto.jpg';
3938
var TYPES = ['U', 'P', 'X'];
40-
39+
40+
var swapImage = function (img) {
41+
if (!img.alt || img.alt.indexOf('Student Photograph') === -1) {
42+
return;
43+
}
44+
var id = img.id || img.parentNode.parentNode.nextSibling.innerHTML || img.parentNode.parentNode.parentNode.nextSibling.innerHTML;
45+
var matricNumber = calculateNUSMatricNumber(id);
46+
var originalSrc = img.src;
47+
var photoUrlPrefix = PHOTO_URL + matricNumber + '&type=';
48+
var typeIndex = 0;
49+
img.width = 170;
50+
img.height = 227;
51+
img.onerror = function () {
52+
img.src = typeIndex < 3 ? photoUrlPrefix + TYPES[typeIndex++] : originalSrc;
53+
};
54+
img.onerror.call();
55+
};
56+
4157
var imgs = document.getElementsByTagName('img');
4258
for (var i = 0; i < imgs.length; i++) {
43-
var img = imgs[i];
44-
if (img.id && img.src === NOPHOTO_URL) {
45-
var matricNumber = calculateNUSMatricNumber(img.id);
46-
if (matricNumber) {
47-
(function () {
48-
var typeIndex = 0;
49-
img.onerror = function () {
50-
this.src = typeIndex < TYPES.length ? PHOTO_URL + matricNumber + '&type=' + TYPES[typeIndex++] : NOPHOTO_URL;
51-
}
52-
img.onerror.call(img);
53-
})();
54-
}
55-
}
59+
swapImage(imgs[i]);
5660
}
5761
})();

0 commit comments

Comments
 (0)