-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahoo-search-jisho-redirect.user.js
67 lines (57 loc) · 2.52 KB
/
yahoo-search-jisho-redirect.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
// ==UserScript==
// @name Yahoo Search Jisho Redirect
// @namespace http://tampermonkey.net/
// @version 0.1.3
// @description Redirects yahoo searches to jisho searches for easy lookups from MCBookViewer
// @author You
// @match *://dic.yahoo.co.jp/search/?ei=UTF-8&p=*
// @match *://dic.yahoo.co.jp/search?ei=UTF-8&p=*
// @match *://dic.yahoo.co.jp/close/?ei=UTF-8&p=*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
const terms = (location.search.match(/p=(.*)/)[1]).replace('&stype=prefix&fr=dic', '');
location.replace('https://jisho.org/search/' + encodeURIComponent(trim(decodeURIComponent(terms))));
})();
function trim(phrase) {
const remove = regex => {
phrase = phrase.replace(regex, '');
};
phrase = phrase.trim();
//dangling punctuation/symbols from the previous sentence
remove(/^[。、!]/);
//dangling commas
remove(/[、]$/);
//if they look up something in english don't count that as a word to review (they can look up the result if they really want it to show up)
remove(/^[\w\d\s]*$/);
//remove messy copying (extra punctuation for other sentences), and remove quotes when they're not necessary for the quote.
//we don't want to remove quotes if it's something like "「うるさいだまれ」って言った" because it changes the meaning.
[ ['「', '」'],
['『', '』'],
['【', '】'],
].forEach(([open, close]) => {
const leadingOpen = new RegExp('^' + open),
leadingClose = new RegExp('^' + close),
trailingClose = new RegExp(close + '$'),
trailingOpen = new RegExp(open + '$');
//starts with an opening quote but doesn't have any closing quotes, assume only part of a quote was selected, no need to keep in quotes
if (leadingOpen.test(phrase) && !phrase.includes(close)) {
remove(leadingOpen);
}
//same with closing quote
if (trailingClose.test(phrase) && !phrase.includes(open)) {
remove(trailingClose);
}
//a full quote with quote marks was included and nothing outside it, just remove the quote, it's unnecessary
if (leadingOpen.test(phrase) && trailingClose.test(phrase)) {
remove(leadingOpen);
remove(trailingClose);
}
//remove any quotes for quotes that weren't included in the phrase
remove(trailingOpen);
remove(leadingClose);
});
return phrase;
}