Repository navigation
Expand file tree
/
Copy pathtingxie.user.js
More file actions
126 lines (111 loc) · 4.14 KB
/
Copy pathtingxie.user.js
File metadata and controls
126 lines (111 loc) · 4.14 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// ==UserScript==
// @name Tingxie Reader
// @include https://translate.google.com/*
// @include http://translate.google.com/*
// @version 0.1
// @extension_id ifjmolgbefhbldkmcjkellbopinimbli
function simulate(element, eventName)
{
var options = extend(defaultOptions, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers)
{
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent)
{
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents')
{
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else
{
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else
{
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}
var nextWordPause = 4000;
var mouseDownPause = 200;
var shuffle = function(array) {
console.log(array.length);
for (var j, x, i = array.length; i; j = parseInt(Math.random() * i), x = array[--i], array[i] = array[j], array[j] = x);
console.log(array);
return array;
}
// speak in vocabulary terms
var speakDefs = function(defs, index) {
if (index < defs.length) {
simulate(defs[index],"mousedown");
// mitigate a race condition
setTimeout(function() {simulate(defs[index],"mouseup");}, mouseDownPause);
setTimeout(function() {speakDefs(defs, index+1);}, nextWordPause);
}
}
var running = false;
var runTingXie = function(words, index) {
if (index == 0 && running) {
return null;
}
running = true;
if (index < words.length) {
running = true;
var defs = words.filter(":eq(" + index + ")").find(".goog-toolbar-button.goog-inline-block.trans-listen-button");
console.log(words.filter(":eq(" + index + ")"));
console.log(defs.length);
speakDefs(defs,0);
setTimeout(function() { runTingXie(words, index+1);}, 2*nextWordPause);
} else {
running = false;
}
};
$(document).ready ( function(){
// flyout the vocabulary list
simulate($(".jfk-button.jfk-button-standard.jfk-button-narrow.pb-sw")[0],"mousedown");
simulate($(".jfk-button.jfk-button-standard.jfk-button-narrow.pb-sw")[0],"mouseup");
// after the vocab list is populated by the AJAX call, then get the review words
var words;
setTimeout(function(){
var button = $('<div></div>', {"class":"goog-inline-block goog-toolbar-button goog-toolbar-button-checked", "aria-pressed":"true", "role":"button", "tabindex":"0", "style":"-webkit-user-select: none;"});
button.append($('<div></div>', {"class":"goog-inline-block goog-toolbar-button-outer-box"}).append($('<div class="goog-inline-block goog-toolbar-butoon-inner-box">听写 by 李睿</div>')));
$('div#pb-tool').append(button[0]);
button.bind("click",function() {
words = $(".nolabel");
words = shuffle(words);
runTingXie(words,0); });
}, 1000);
});