-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognizers.js
220 lines (197 loc) · 6.7 KB
/
recognizers.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
var SINGLE='single', DOUBLE='double';
var sefariaMatchers = {
DAFYOMIPORTAL: {
pattern: /^.*daf-yomi.com\/(DafYomi_Page\.aspx)/mgi,
getReference: function (uri) {
return {
reference: $("#ContentPlaceHolderMain_hdrMassechet").text(),
mode:SINGLE,
site: 'Portal Daf Yomi'
};
}
},
DAFHACHAIM: {
pattern: /^.*dafhachaim.org\/(daf|resources)\/?.*/mgi,
getReference: function (uri) {
var ref=null;
return {
reference: $(".here-i-am").text(),
mode:DOUBLE,
site: 'Daf HaChaim'
};
}
},
EDAF: {
pattern: /^.*e-daf.com\/?$|^.*e-daf.com\/index\.asp(\?.*)?$/mgi,
getReference: function (uri) {
return {
reference: document.title.replace("E-DAF.com ", "").toLowerCase(),
mode:SINGLE,
site: 'E-DAF.com'
};
}
},
ALLDAF: {
pattern: /^.*alldaf.org\/p\/[0-9]+(\?.*)?$/mgi,
getReference: function (uri) {
var ref=getAllDafReference();
return {
reference: ref,
mode:DOUBLE,
site: 'AllDaf'
};
}
},
REALCLEARDAF: {
pattern: /^.*realcleardaf.com\/(.*)$/mgi,
getReference: function (uri) {
var title=$('h1.shiur-title');
if(!title || title.text()==null|| title.text()=="") return null;
else title=title.text();
return {
reference: title.substring(title.indexOf("|")+1),
mode:DOUBLE,
site: 'RealClearDaf'
};
}
},
STEINSALTZ: {
pattern: /^.*steinsaltz-center.org\/vault\/DafYomi\/.*$/mgi,
getReference: function (uri) {
console.log(uri=(window.top?window.top:window).location.href);
console.log("steinsaltz: "+uri);
var title=(""+uri).replace(/.*\/vault\/DafYomi\/([^\.]+).*/,"$1");
console.log("title: "+title);
if(!title || title.length==0)return null;
return {
reference:title.replace("_"," "),
mode:DOUBLE,
site: 'Steinsaltz PDF'
};
}
},
YUTORAH_SHIUR: {
pattern: /^.*yutorah.org\/lectures\/lecture\.cfm(.*)$/mgi,
getReference: function (uri) {
var title=$('h2[itemprop=name]');
if(!title || title.text()==null|| title.text()=="") return null;
else title=title.text();
if( ! /(.*)( Daf )[0-9]+/.test(title)) return null;
else title=title.replace(" Daf","");
return {
reference: title,
mode:DOUBLE,
site: 'YUTorah'
};
}
},
YUTORAH_DAF: {
pattern: /^.*yutorah.org\/daf\.cfm.*$/mgi,
getReference: function (uri) {
var title=$('.daf-title span');
if(!title || title.length==0) return null;
title=title.text();
var lastChar=(title.charAt(title.length-1));
if(lastChar == 'a' || lastChar=='b') title=title.substring(0,title.length-1);
return {
reference: title,
mode:DOUBLE,
site: 'YUTorah On the Daf'
};
}
},
HADRAN: {
pattern: /^.*hadran.org.il\/daf\/(.*)$/mgi,
getReference: function (uri) {
var title=$('title');
if(!title || title.text()==null || title.text().indexOf(" - ")==-1) return null;
else title=title.text().substring(0,title.text().indexOf(" - "));
if( ! /(.*) [0-9]+/.test(title)) return null;
return {
reference: title,
mode: DOUBLE,
site: 'Hadran'
};
}
},
OUTORAH: {
pattern: /^.*outorah.org\/dafImage\/([^\/]+)\/([0-9]+)\/([0-1])?$/mgi,
getReference: function (uri, self) {
var ref=uri.replace(self.pattern, function(str, masechta, daf, amud){
return masechta +" "+daf+((amud=='0')?'a':'b');
});
return {
reference: ref,
site: 'OuTorah Daf Image'
};
}
},
};
function getSefariaReference(uri){
var uri = uri.toLowerCase();
console.log("My URI " + uri);
var result=null;
Object.values(sefariaMatchers).forEach(function (matcher) {
var match=checkSefariaReference(uri,matcher);
if(match) result=match;
});
console.log("Get SefRef Returning "+result);
return result;
}
function checkSefariaReference(uri, matcher) {
console.log("Matching "+uri+" "+matcher.pattern);
var matches = false;
if (matcher.pattern instanceof RegExp) {
matches = matcher.pattern.test(uri);
matcher.pattern.lastIndex=0; // reset the internal state to allow for reuse
} else {
matches = (uri.indexOf(matcher.pattern.toLowerCase()) > -1);
}
console.log("Matches : " + matches);
if(matches) {
var ref= matcher.getReference(uri, matcher);
console.log("Reference: "+JSON.stringify(ref));
return ref;
}
return null;
}
// Special Treatment for AllDAf which does inline DOM loading instead of normal page loads
function getAllDafReference(){
console.log("Get AllDaf Reference");
var ref="";
$(".multiselect__single").each((x,y)=>{ref+=$(y).text()+" ";});
console.log("AllDaf REF:"+ref);
return ref.trim();
}
var alldaf_current=null;
var current_url=location.href;
// Alldaf specific functionality
if(checkSefariaReference(current_url.toLowerCase(),sefariaMatchers.ALLDAF) !=null)
{
console.log("Found Sefaria Reference for ALLDAF - Enabling special Features");
alldaf_current=getAllDafReference();
function alldafChanged () {
var ref=getAllDafReference();
console.log(alldaf_current +" == "+ref+"?");
if(alldaf_current !=ref){
console.log("[[ AllDaf Change Event ]] >> "+ref);
// it's not the same so lets trash the sidebar
removeSidebar();
} else if(ref!=null) {
console.log("[[ AllDaf - Same Daf, so lets leave] alone]");
return;
}
if( /^(.* [1-9]+)( \-.*)?$/.test(ref)){
// It is a valid daf though
console.log("[[ AllDaf Change Event ]] >> "+ref);
alldaf_current=ref;
refreshExtension(alldaf_current, DOUBLE);
}
}
setInterval(function(){
if(location.href!=current_url){
current_url=location.href;
alldafChanged();
}
},2000);
}