forked from SiTLar/pyt-vanilla-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
254 lines (244 loc) · 6.5 KB
/
init.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
"use strict";
define(
[
"./utils"
, "./common"
, "./draw"
, "./actions"
, "./router"
, "./hasher"
, "./faselector"
],function(
Utils
, _Common
, _Drawer
, _Actions
, _Router
, _Hasher
, _FaSelector
){
var apis = {
"freefeed": require("./freefeed")
,"mokum": require("./mokum")
}
var gTemplates = require("./templates.json");
var gDomains = require("./domains.json");
function _gMe(){
var context = this;
var logins = context.logins;
var mainId;
var ids = Object.keys(logins);
if(ids.length == 0)return null;
if(ids.length === 1){
mainId = ids[0];
context.token = logins[ids[0]].token;
}else ids.some(function(id){
if(logins[id].token == context.token) {
mainId = id;
return true;
}else return false;
});
logins[mainId].data.domain = context.domain;
return logins[mainId].data;
}
function _ids(){
var ids = Object.keys(this.logins);
return ids;
}
function _Context(v, domain, api){
var context = this ;
Object.keys(context.defaults).forEach(function(key){
context[key] = JSON.parse(JSON.stringify(context.defaults[key]))
});
context.domain = domain;
context.cView = v;
v.contexts[domain] = context;
context.api = new Object();
Object.keys(api.protocol).forEach(function(f){
context.api[f] = function(){
return api.protocol[f].apply(context, arguments)
.then(function(res){
return ( typeof res !== "undefined")?
api.parse(res)
:null;
});
}
});
context.api.parse = api.parse;
context.api.name = api.name;
this.p = new Utils._Promise( function(resolve){resolve()});
var rtFuncs = ["rtSubTimeline","rtSubPost","rtSubUser"];
if ((typeof api.oRT !== "undefined") && JSON.parse(v.localStorage.getItem("rt"))){
context.rt = new api.oRT(context,JSON.parse(v.localStorage.getItem("rtbump")));
rtFuncs.forEach(function(key){
context[key] = function(inp){
context.rt[key](inp);
};
});
}else rtFuncs.forEach(function(key){context[key] = function(){};});
Object.defineProperty(this, "gMe", {"get": _gMe});
Object.defineProperty(this, "ids", {"get": _ids});
}
_Context.prototype = {
constructor:_Context
,"defaults":{
"gUsers": { "byName":{}}
,"gUsersQ": {}
,"gComments": {}
,"gAttachments": {}
,"gFeeds": {}
,"gRt": {}
,"miscRts": {}
,"logins": {}
,"token": null
,"pending":[]
,"timelineId": null
}
,"initRt": function(){
var cView = this;
var bump = cView.localStorage.getItem("rtbump");
this.gRt = new RtUpdate(this.token, bump);
this.gRt.subscribe(this.rtSub);
}
,"digestText":function(text){
var context = this;
var arr = context.cView.autolinker.link(
text.trim().replace(/&/g,"&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/[\n\r]{2,}/g, "\r\r")
,"array"
).reduce(function(prev,curr){return prev.concat(curr);},[]);
arr.forEach(function(txt,idx,arr){
arr[idx] = txt.replace(
/___CONTEXT_PATH___/g
,gConfig.front+"as/"+context.domain
).replace(/(https?:\/\/)?friendfeed.com\/\w+/g
,"https://freefeed.net/archivePost?url=$&"
);
});
arr.toString = function(){return this.join(" ");};
return arr;
}
,"getWhoami": function(token){
var context = this;
return context.api["_getWhoami"](token).then(function(res){
var id = res.users.id;
if(typeof context.logins[id] === "undefined"){
context.logins[id] = new Object();
context.logins[id].token = token;
context.logins[id].domain = context.domain;
}
context.logins[id].data = res;
context.logins[id].data.domain = context.domain;
context.cView.Common.refreshLogin(id,context);
return res;
});
}
}
function _cView(doc){
var cView = this;
Object.keys(cView.defaults).forEach(function(key){
cView[key] = JSON.parse(JSON.stringify(cView.defaults[key]))
});
//cView.autolinker = new Autolinker({"truncate":20, "replaceFn":Utils.frfAutolinker } );
["localStorage", "sessionStorage"].forEach(function(storage){
cView[storage] = new Object();
["setItem", "getItem", "removeItem"].forEach(function(action){
cView[storage][action] = function(){
try{
return window[storage][action].apply(window[storage],arguments);
} catch(e){return null};
}
});
});
cView.doc = doc;
doc.cView = cView;
cView.cView = cView;
cView.Common = new _Common(cView);
cView.Drawer = new _Drawer(cView);
cView.Actions = new _Actions(cView);
cView.Router = new _Router(cView);
cView.hasher = new _Hasher["_Minhash"]({"fnum":1000});
cView.FaSelector = new _FaSelector(cView);
cView.addons = {
"all":new Array()
,"ok": function(){}
,"pr": new Object()
};
cView.addons.pr = new Utils._Promise(function(resolve){
cView.addons.ok = resolve;
});
//cView.SecretActions = new _SecretActions(cView);
var Url2link = require("./url2link");
cView.autolinker = new Url2link({ "truncate":25
,"url":{
"actions":[
["pre",cView.Common.setFrontUrl]
]
}
,"text":{
"actions":[
["post",function(text){
return text.replace(/\n/g," \n").split(" ")
.filter(function(txt){return txt!="";});
}]
]
}
,"hashtag":{
"actions":[
["post",function(text){
return text.replace(
/___CONTEXT_SEARCH___/
, gConfig.front+"search?qs="
);
}]
]
}
});
var domains = new Object();
var confDomains = Array.isArray(gConfig.domains)?gConfig.domains:Object.keys(gConfig.domains);
//gConfig.domains.forEach(function (d){
confDomains.forEach(function (d){
var cfg = gDomains[d];
domains[d] = cfg;
cView.contexts[d] = new _Context(cView, d, apis[cfg.api](cfg.server));
});
gConfig.domains = domains;
cView.leadContext = cView.contexts[gConfig.leadDomain];
}
_cView.prototype = {
constructor: _cView
,"defaults":{
"contexts":{}
,"gEmbed": {}
,"gNodes": {}
,"rtSub" : {}
,"cTxt": null
,"subReqsCount":0
,"blocks": {"blockPosts":{},"blockComments":{},"blockStrings":{}}
,"blockLists": {"cmts":"blockComments", "posts":"blockPosts"}
,"threshold":0.63
,"skip":0
,"minBody": 20
,"noBlocks":false
}
,"Utils":Utils
};
function init(doc){
var cView = new _cView(doc);
cView.Common.genNodes(gTemplates.nodes).forEach( function(node){
cView.gNodes[node.classList[0]] = node;
});
if(cView.Common.getCookie("privacy")){
var nodePriv = cView.doc.getElementById("privacy-popup");
nodePriv.parentNode.removeChild(nodePriv);
}
cView.Common.setIcon("throbber-16.gif");
return cView;
}
return {
"cView":_cView
,"init":init
}
});