-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathword_count.user.js
More file actions
53 lines (47 loc) · 1.41 KB
/
Copy pathword_count.user.js
File metadata and controls
53 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// ==UserScript==
// @name Speedgrader Word Count
// @namespace https://github.com/UCBoulder
// @description Displays a word count for documents within Speedgrader
// @match https://canvadocs.instructure.com/*
// @require https://code.jquery.com/jquery-1.12.4.min.js
// @grant none
// @run-at document-idle
// @version 1.0.4
// ==/UserScript==
/* globals $ */
function getWordCount() {
var wc = 0;
$('div.textLayer > span').each(function(divIndex) {
wc += $(this).text().split(/\s+\b/).length;
});
return wc;
}
function updateWordCount(idleFrames, lastWC) {
var wc = getWordCount();
if (wc === lastWC) {
idleFrames++;
} else {
$('#swc_display').text(`Word count: ${wc}`);
idleFrames = 0;
}
// If WC doesn't update for 5 seconds, stop checking
if (idleFrames < 10) {
setTimeout(function () { updateWordCount(idleFrames, wc); }, 500);
}
}
function waitForElement(selector, callback) {
if ($(selector).length) {
callback();
} else {
setTimeout(function() {
waitForElement(selector, callback);
}, 100);
}
}
(function() {
'use strict';
waitForElement('div.textLayer', function() {
$('#App > nav > div > div.ViewerControls--title').append('<span id="swc_display" style="margin-right: 1em">Word count:</span>');
updateWordCount(0, 0);
});
})();