-
Notifications
You must be signed in to change notification settings - Fork 4
/
shanbay.user.js
664 lines (627 loc) · 21.8 KB
/
shanbay.user.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
function main(option) {
// ---------------------------------------------批量添加
$('#add-learnings .instruction').append('<font color="blue">已解除10个词的限制(每次依然仅提交10个,但多出的词会保留在文本框内,以备再次提交)</font>');
$('#add-learnings-form').unbind('submit').submit(function() {
var form = $(this);
var textarea = form.find('textarea');
var url = form.find('actions').text();
var words = $.trim(textarea.val());
form.find('.msg').hide();
if (words.length == 0) {
$('#error1').show();
textarea.focus();
return false;
}
var wordsArr = words.split('\n');
var remains = '';
if (wordsArr.length > 10) {
remains = wordsArr.slice(10).join('\n');
words = wordsArr.slice(0, 10).join('\n');
}
form.find('input[type=submit]').hide();
form.find('.loading').show();
$.getJSON(url, {words: words}, function(data) {
form.find('input[type=submit]').show();
form.find('.loading').hide();
if (data.result > 0) {
$('#error2').html(data.note).show();
return false;
}
var notfound_words = data.notfound_words;
if (notfound_words.length > 0) {
$('.notfounds').show();
$('.notfounds ul').html('');
$.each(notfound_words, function(k, word) {
$('.notfounds ul').append($('#added-failed_tmpl').tmpl({word: word}));
});
} else {
$('.notfounds').hide();
}
var learning_dicts = data.learning_dicts;
if (learning_dicts.length > 0) {
$('.learnings').show();
$('.learnings ul').html('');
$.each(learning_dicts, function(k, learning) {
var item = $('#added-learning_tmpl').tmpl({id: learning.id,pronunciation: learning.pronunciation,content: learning.content,has_audio: learning.has_audio,definition: learning.definition,uk_audio: learning.uk_audio,us_aduio: learning.us_audio});
item.find('.definition').hide();
$('.learnings ul').append(item);
$.getJSON('/api/v1/bdc/search/?word='+learning.content,function(r){
if (r.data.retention > 0) {
var retentionEle = $(''+
'<div><div class="retention-progress" style="width: 200px;float: left;">'+
'<div class="retention-reviewed" style="width: 0%"> <div class="bar" style="width: 100%;">0</div> </div>'+
'<div class="retention-left"> <div class="bar" style="width: 100%;"></div> </div>'+
'</div><button style="margin-left: 10px;">我忘了</button> </div>'+
'');
item.append(retentionEle);
retentionEle.find('.retention-reviewed').css({
width:Math.min(r.data.retention*100/r.data.target_retention,100)+'%'
}).find(".bar").text(r.data.retention);
retentionEle.find('button').click(function(e){
$.ajax({
type:"PUT",
url:"/api/v1/bdc/learning/"+r.data.learning_id+"/",
data:{retention:1},
success:function() {
retentionEle.remove();
item.find('.definition').show();
}});
});
}
else {
item.find('.definition').show();
}
});
});
$('.speaker').click(function() {
if (auto_play_mode == 2)
var audio_url = $(this).parent().attr('uk_audio');
else
var audio_url = $(this).parent().attr('us_audio');
play_mp3(audio_url);
});
} else {
$('.learnings').hide();
}
textarea.focus().val(remains);
})
return false;
});
// ----------------------------------阅读查词限制.
/*
var reader_nav = $('.reader-nav');
if (option.LENGTH_PER_QUERY>0 && reader_nav.length) {
reader_nav.append('<span style="position:relative;left:50px;">剩余查词:<b class="query-number" style="font-size:20px;color:#ec4272"></b></span>');
var old_parse = Word.prototype.parse;
var queryed = {};
Word.prototype.parse = function(response) {
var queryNum = $('.reader-nav').find('.query-number').text();
if (response.status_code == 0 && !(data.content in queryed)) {
$('.reader-nav').find('.query-number').text(queryNum-1);
queryed[data.content] = 1;
}
return old_parse.apply(this, arguments);
};
var lastLength = 0;
var lisenerID = setInterval(function(){
if (app.article && app.article.attributes.data) {
// option.LENGTH_PER_QUERY: 多少个查一次
var length = app.article.attributes.data.length;
if (length != lastLength) {
lastLength = length;
var totalNum = Math.ceil(length / option.LENGTH_PER_QUERY);
reader_nav.find('.query-number').text(totalNum);
}
//clearInterval(lisenerID);
}
},2000);
}// end of 查词限制
*/
// ---------------------------------------跳过新版步骤
if (option.SKIP_REVIEW_MODE > 0 && typeof ReviewView != 'undefined') {
ReviewView.prototype.render_detail = function(prev_mode, result) {
stop_spin();
var review_detail_view = new ReviewDetailView({model: this.model,review_view: this,prev_mode: prev_mode,result: result});
review_detail_view.bind('next', this.next, this);
review_detail_view.bind('prev', this.prev, this);
if (this.options.mode == 'learning') {
$('.progress-box').hide();
}
if (G.forget_num) {
review_detail_view.known_word();
}
};
}
}// end of main 整个main将被注入
// 获取选项后注入到主页面
sendMessage({
action: "getOptions"
}, function(response) {
var script = document.createElement("script");
var option = JSON.stringify(response);
script.textContent = "(" + main.toString() + ")(" + option + ");//@ sourceURL=shanbay_plus.js";
document.body.appendChild(script);
});
/*
var script = document.createElement("script");
var option = JSON.stringify(getOption());
script.textContent = "(" + main.toString() + ")(" + option + ");//@ sourceURL=shanbay_plus.js";
document.body.appendChild(script);
*/
var word_trigger = {};
var curword = '';
// 监视#review的变动
var target = document.querySelector('#review');
if (target) {
var observer = new MutationObserver(function(mutations) {
// 预加载
var word = $('#current-learning-word').text();
if (word) {
curword = word;
showVocabulary(word, true);
addVocabularySample(word);
return;
}
word = $('#preview h1').eq(0).text();
if (word.length && !word_trigger[word]) {
word_trigger[word] = 1;
console.log("show vocabulary, trigger");
showVocabulary(word, false);
}
});
observer.observe(target, { childList: true });
}
//------------------------------------ 显示助记信息
function showVocabulary(word, show) {
if (word.length) {
sendMessage({
action: "getOptions"
}, function(option) {
var pos = option.SHOW_VOCABULARY_HELP;
if (pos <= 0) return;
sendMessage({
action: "getVocabulary",
word: word
}, function(response) {
console.log("show vocabulary, response");
if (show && word == curword) {
showVocabularyResponse(word, response, pos - 1);
}
});
});
}
}
var g_expandVocabulary = false; // 默认情况不展开
function showVocabularyResponse(word, response, pos) {
var content = $(response).find('.blurb');
if (content.length) {
$('#review-definitions .cndf .text').css({'color': '#f7f7f7','cursor':'pointer'}).click(function(){$(this).css({'color':'',cursor:''})});
}
if (!content.length && response.length < 10) {
// 显示错误信息
content = $('<span class="error" style="color:gray;cursor:pointer"></span>').text(response);
}
if (true) {
var readMore = content.find(".readMore");
if (readMore.length) {
readMore.attr('href', 'http://www.vocabulary.com' + readMore.attr('href'));
readMore.attr('target', '_blank');
}
var addReview = $(
'<div id="shanbayplus_add_review" class="row">'+
'<div class="span1"><h6 class="pull-right">助记</h6></div>'+
'<div class="span9"><div class="well" style="overflow:hidden">'+
'<div class="span6" style="margin-left:0px;"></div>'+
'<div class="expand" style="text-align:right"><span style="color:#209e85;cursor:pointer;" title="快捷键:m">展开/收起</span></div>'+
'</div></div></div>'
);
addReview.find('.span6').append(content);
if (!g_expandVocabulary) {
content.find('.long').hide();
content.find('.sidebar').hide();
}
if (!pos) {
$('#learning_word .word').children().eq(1).before(addReview);
}
else {
$('#review .learning-detail-container').children().eq(pos).before(addReview);
}
var toggle = function() {
g_expandVocabulary = !g_expandVocabulary;
content.find('.long').toggle();
content.find('.sidebar').toggle();
}
addReview.find('.expand span').click(toggle);
addReview.find('.error').click(function() {
addReview.remove();
showVocabulary(word, true);
});
$.Shortcuts.stop();
$.Shortcuts.empty();
$.Shortcuts.add({
type: 'up',
mask: 'm',
handler: toggle
}).start();
}
}
//-------------------vocabulary.com examples----------------------------
function addVocabularySample(word)
{
var tab = $('<li><a class="ex-voc-box-tab" href="#ex-voc-box">Vocabulary例句</a></li>');
var tab_content = $('<div class="tab-pane ex-group" id="ex-voc-box"><ol></ol></div>');
$('#example-tab').append(tab);
$('#example-tab').next().append(tab_content);
tab.click(function() {
showVocabularySample(tab_content);
});
tab_content.data('offset', 0);
tab_content.data('word', word);
}
function showVocabularySample(container)
{
var word = container.data('word'),
offset = container.data('offset');
if (!container.find('li').length) {
sendMessage({
action: "getVocabularyExamples",
word: word,
offset: offset
}, function(data) {
//data = JSON.parse(response);
var ul = container.find('ol');
if (data.result.sentences) {
data.result.sentences.forEach(function(ele, i) {
renderHTML(ele).append(link(ele)).appendTo(ul);
});
ul.find('li').mouseenter(function(e) {
var btn = $('<div class="actions btn-group pull-right"><a class="btn btn-mini" href="javascript:void(0)">添加到自建例句</a></div>');
$(e.currentTarget).append(btn);
btn.click(function(e) {
var li = $(e.currentTarget).parent();
var tab = $('#example-tab').find('li a.ex-voc-box-tab').parent().prev().find('a');
setTimeout(function() {
tab[0].click();
setTimeout(function() {
var ori = li.find('.sentence').text();
var trans = li.find('.source').text();
$('#ex-create').find('#newex').val(ori);
$('#ex-create').find('#newtrans').val(trans);
}, 1);
}, 1);
/*
var ori = li.find('.sentence').text();
var trans = li.find('.source').text();
getContentIdByWord(word, function(content_id) {
addExample(ori, trans, content_id, function(){});
});
*/
});
}).mouseleave(function(e) {
$(e.currentTarget).find('.actions').remove();
});
}
});
}
}
function addExample(ori, trans, content_id, callback)
{
$.post('/api/v1/bdc/example/', {
original: ori,
translation: trans,
vocabulary: content_id
}, function(r) {
callback(r);
});
}
function getContentIdByWord(word, callback)
{
$.getJSON('/api/v1/bdc/search/', {word:word}, function(r) {
callback(r.data.content_id);
});
}
function renderHTML(result) {
var sent = $('<div class="sentence"/>');
var str = result.sentence;
var offsets = result.offsets;
var last = 0;
for (var i = 0; i < offsets.length; i += 2) {
sent.append(document.createTextNode(str.substring(last, offsets[i])));
sent.append($("<strong/>").text(str.substring(offsets[i], offsets[i + 1])));
last = offsets[i + 1]
}
sent.append(document.createTextNode(str.substring(last)));
return $("<li/>").append(sent)
}
function link(result) {
var sourceLink = $('<a target="_blank" class="source"></a>');
var $title = $("<span />").appendTo(sourceLink);
if (result.volume.corpus.id == "GUT") {
sourceLink.attr("href", "http://www.gutenberg.org/ebooks/" + result.volume.locator);
$title.addClass("author").text(result.volume.author)
} else {
if (result.volume.corpus.id == "LIT") {
$title.addClass("title").text(result.volume.title)
} else {
$title.addClass("corpus").text(result.volume.corpus.name);
if (result.volume.datePublished) {
$('<span class="date" />').text((new Date(Date.parse(result.volume.datePublished))).toDateString()).appendTo(sourceLink)
}
}
if (result.volume.asin) {
sourceLink.attr("href", "http://www.amazon.com/dp/" + result.volume.asin + "?tag=vocabularyc00-20")
} else {
if (result.volume.locator && result.volume.locator.indexOf("http") == 0) {
sourceLink.attr("href", result.volume.locator)
}
}
}
return sourceLink
}
// 全自动创建单词书
$(function() {
if (!$('.btn-add-new-unit-container').length) return;
var container = $('<div><p style="color: #209E95; font-weight: bold;">批量添加格式:单词\\t例句\\t例句解释</p>'+
'<p style="color: #209E95; font-weight: bold;">当前单元:<span></span></p></div>');
var wordForm = $('<form action="/api/v1/wordlist/vocabulary/" method="post">'+
'<textarea name="word"></textarea>'+
'<input type="submit" class="btn btn-success" value="提交">'+
'</form>'),
notFound = $('<div class="hide"><h4>下列单词未找到</h4><ul></ul></div>'),
dup = $('<div class="hide"><h4>下列单词已经存在</h4><ul></ul></div>'),
example_failed = $('<div class="hide"><h4>下列单词例句添加失败</h4><ul></ul></div>'),
notAdd = $('<div class="hide"><h4>框中剩余单词不予添加(数量限制?)</h4><ul></ul></div>');
var cur_wordlist_id, cur_unit_name, cur_unit;
container.append(wordForm).append(notFound).append(dup).append(notAdd).append(example_failed);
$('.btn-add-new-unit-container').after(container);
$('#wordbook-wordlist-container').delegate('.wordbook-create-candidate-wordlist', 'click', function(evt) {
cur_unit = $(evt.currentTarget);
cur_wordlist_id = cur_unit.find('a.btn-update-unit-info').attr('unit-id');
container.find("span").text(cur_unit.find(".wordbook-wordlist-name").text());
$(evt.currentTarget).siblings().removeClass("highlight").end().addClass("highlight");
cur_unit.after(container);
});
wordForm.submit(function() {
var form = $(this);
var textarea = form.find('textarea');
var url = form.attr('action');
var lines = $.trim(textarea.val());
var line, word;
if (!cur_wordlist_id) {
alert('请先选中单元');
return false;
}
if (lines.length == 0) {
textarea.focus();
return false;
}
form.find('input[type=submit]').hide();
lines = lines.split("\n");
textarea.focus().val('');
notFound.hide().find('ul').empty();
notAdd.hide().find('ul').empty();
dup.hide().find('ul').empty();
example_failed.hide().find('ul').empty();
var finish_add = function(lines) {
if (lines.length) {
notAdd.show();
}
form.find('input[type=submit]').show();
textarea.focus().val(lines.join('\n'));
}
var add_next = function(lines) {
if (lines.length == 0) {
return finish_add(lines);
}
line = lines.shift();
var arr = line.split("\t");
var word = arr[0];
$.post(url, {word: word, id: cur_wordlist_id}, function(res) {
if (res.status_code != 0) {
// 没找到
if (res.status_code == 404) {
notFound.show().find('ul').append('<li>'+word+'</li>');
}
else if (res.msg.match("存在")) {
// 单词已经存在
dup.show().find('ul').append('<li>'+word+'</li>');
}
// 其他原因
else if (true) {
lines.unshift(line);
return finish_add(lines);
}
}
else {
var num_el = cur_unit.find('.wordbook-create-wordlist-title span').parent();
var num_str = num_el.html().replace(/\d+/, function(n) {
return parseInt(n) + 1;
});
num_el.html(num_str);
if (arr.length > 2) {
return addExample(arr, res.data.vocabulary.id, lines);
}
}
add_next(lines);
});
}
var addExample = function(arr, voc_id, lines) {
$.post('/wordlist/vocabulary/example/add/', {
vocabulary_id:voc_id,
wordlist_id:cur_wordlist_id,
original:arr[1],
translation:arr[2]
}, function(data) {
if (data.status != 0) {
example_failed.show().find('ul').append('<li>'+line+'</li>');
return add_next(lines);
}
sendMessage({
action: "putExample",
word: voc_id,
example_id: data.example.id
}, function(result) {
add_next(lines);
});
});
}
add_next(lines);
return false;
});
});
// 批量加词
$(function() {
if (!$('#add-learnings-form').length) return;
$('#add-learnings').prepend('<p id="add_example"><button class="fresh">为新词添加例句</button><button class="familiar">为正在学习单词添加例句</button><button class="today">为今日单词添加例句</button></p>');
$('#add_example button').click(likeExampleFromList);
/*
var form = $('#add-learnings-form').clone();
$('#add-learnings-form').replaceWith(form);
$('#add-learnings .instruction').append('<font color="blue">已解除10个词的限制(每次依然仅提交10个,但多出的词会保留在文本框内,以备再次提交)</font>');
$('#add-learnings-form').unbind('submit').submit(function() {
var form = $(this);
var textarea = form.find('textarea');
var url = form.find('actions').text();
var words = $.trim(textarea.val());
form.find('.msg').hide();
if (words.length == 0) {
$('#error1').show();
textarea.focus();
return false;
}
var wordsArr = words.split('\n');
var remains = '';
if (wordsArr.length > 10) {
remains = wordsArr.slice(10).join('\n');
words = wordsArr.slice(0, 10).join('\n');
}
form.find('input[type=submit]').hide();
form.find('.loading').show();
$.getJSON(url, {words: words}, function(data) {
form.find('input[type=submit]').show();
form.find('.loading').hide();
if (data.result > 0) {
$('#error2').html(data.note).show();
return false;
}
var notfound_words = data.notfound_words;
if (notfound_words.length > 0) {
$('.notfounds').show();
$('.notfounds ul').html('');
$.each(notfound_words, function(k, word) {
$('.notfounds ul').append($('#added-failed_tmpl').tmpl({word: word}));
});
} else {
$('.notfounds').hide();
}
var learning_dicts = data.learning_dicts;
if (learning_dicts.length > 0) {
$('.learnings').show();
$('.learnings ul').html('');
$.each(learning_dicts, function(k, learning) {
var item = $('#added-learning_tmpl').tmpl({id: learning.id,pronunciation: learning.pronunciation,content: learning.content,has_audio: learning.has_audio,definition: learning.definition,uk_audio: learning.uk_audio,us_aduio: learning.us_audio});
item.find('.definition').hide();
$('.learnings ul').append(item);
$.getJSON('/api/v1/bdc/search/?word='+learning.content,function(r){
if (r.data.retention > 0) {
var retentionEle = $(''+
'<div><div class="retention-progress" style="width: 200px;float: left;">'+
'<div class="retention-reviewed" style="width: 0%"> <div class="bar" style="width: 100%;">0</div> </div>'+
'<div class="retention-left"> <div class="bar" style="width: 100%;"></div> </div>'+
'</div><button style="margin-left: 10px;">我忘了</button> </div>'+
'');
item.append(retentionEle);
retentionEle.find('.retention-reviewed').css({
width:Math.min(r.data.retention*100/r.data.target_retention,100)+'%'
}).find(".bar").text(r.data.retention);
retentionEle.find('button').click(function(e){
$.ajax({
type:"PUT",
url:"/api/v1/bdc/learning/"+r.data.learning_id+"/",
data:{retention:1},
success:function() {
retentionEle.remove();
item.find('.definition').show();
}});
});
}
else {
item.find('.definition').show();
}
});
});
} else {
$('.learnings').hide();
}
textarea.focus().val(remains);
})
return false;
});
*/
});
function likeExampleFromList(e) {
var el = $(e.currentTarget);
var saved = '';
var finish = function() {
el.text(saved);
el.removeAttr('disabled');
};
var i = 0;
var start = function() {
saved = el.text();
el.attr('disabled', 'disabled');
}
var step = function() {
el.text(++i);
}
var getList = function(type, page) {
$.get('/api/v1/bdc/library/'+type+'/', {page: page, '_': Math.random()}, function(r) {
var nextpage = function() {
if (page * r.data.ipp < r.data.total) {
getList(type, page + 1);
}
else {
finish();
}
};
wait(r.data.objects, function(item, callback) {
likeExample(item.id, function() {
step();
callback();
});
}, nextpage);
if (r.status_code != 0) {
finish();
return;
}
});
};
start();
getList(el.attr('class'), 1);
}
var wait = function(items, check, finish) {
if (items.length > 0) {
check(items[0], function() {
wait(items.slice(1), check, finish);
});
} else {
finish && finish();
}
};
function likeExample(word, callback) {
sendMessage({
action: "getExample",
word: word
}, function(result) {
if (result != undefined) {
wait(result, function(item, callnext) {
$.post('/api/v1/bdc/learning_example/', {"example_id":item}, function() {
callnext();
});
}, callback);
}
else {
callback();
}
});
}