-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFunell.user.js
214 lines (167 loc) · 8.02 KB
/
Funell.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
// General variables
var username, getUser, drafts, draftsReverse, draft, draftsCount,
currentDraftKey, draftKey, currentURI, currentURIHash
// Elements variables
var _currentPointHeader, _draftsIconCntr, _draftsIconLink,
_draftsIconCount, _draftsIconImage, _currentPointFormAdd,
_draftButtonSaveCntr, _draftAlertSuccess, _draftAlertDanger,
_currentPointUserTabs, _draftsTabCntr, _draftsTabLink,
_draftsCntr, _draftCntr, _draftLink, _draftTitle
// General value
getUser = document.querySelector('.headerLinks #usrAvatar')
currentURI = window.location.href
currentURIHash = window.location.hash
draftKey = generationKey()
// General function
function generationKey () {
if (currentURI.indexOf('#draft-') > -1) {
return currentURIHash.split('-')[1]
}
var code = '',characters,i
characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (i = 0; i < 6; i++) {
code += characters.charAt(Math.floor(Math.random() * characters.length))
}
return code
}
function saveDraft (e) {
e.preventDefault()
var drafts, draft, alertCntr, inputTitle, inputContent,
inputCreatedAt, inputUpdateAt, date, dateNow
if (_draftAlertDanger) {
_draftAlertDanger.remove()
}
if (_draftAlertSuccess) {
_draftAlertSuccess.remove()
}
drafts = JSON.parse(localStorage.getItem('DraftsHusobIO'))
if (!drafts) {
drafts = {}
}
date = new Date()
dateNow = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds())
inputTitle = document.querySelector("#new_post input[name='post[title]']").value
inputContent = document.querySelector("#new_post textarea[name='post[content]']").value
if (inputTitle === '' || inputContent === '') {
_draftAlertDanger = document.createElement('div')
_draftAlertDanger.setAttribute('class', 'alert alert-danger')
_draftAlertDanger.setAttribute('style', 'padding: 7px 14px;font-size: 16px;margin: -10px 0 10px;')
_draftAlertDanger.textContent = 'الرجاء تعبئة كل مِن حقل العنوان والمحتوى، قبل حفظ المسودة!'
alertCntr = document.querySelector('#new_post .addBtns')
alertCntr.insertBefore(_draftAlertDanger, alertCntr.firstChild)
return false
}
if (drafts[draftKey]) {
draft = drafts[draftKey]
inputCreatedAt = draft.created_at
} else {
inputCreatedAt = dateNow
}
inputUpdateAt = dateNow
drafts[draftKey] = {
'title': inputTitle,
'content': inputContent,
'created_at': inputCreatedAt,
'update_at': inputUpdateAt
}
localStorage.setItem('DraftsHusobIO', JSON.stringify(drafts))
_draftsIconCount.textContent = Object.keys(drafts).length
_draftAlertSuccess = document.createElement('div')
_draftAlertSuccess.setAttribute('class', 'alert alert-success')
_draftAlertSuccess.setAttribute('style', 'padding: 7px 14px;font-size: 16px;margin: -10px 0 10px;')
_draftAlertSuccess.textContent = 'تم حفظ المسودة بنجاح!'
alertCntr = document.querySelector('#new_post .addBtns')
alertCntr.insertBefore(_draftAlertSuccess, alertCntr.firstChild)
}
if (getUser) {
username = getUser.firstElementChild.getAttribute('href').split('/')[2]
drafts = JSON.parse(localStorage.getItem('DraftsHusobIO'))
if (!drafts) {
draftsCount = 0
} else {
draftsCount = Object.keys(drafts).length
}
_draftsIconCntr = document.createElement('span')
_draftsIconCntr.setAttribute('class', 'hdrMenus pull-left')
_draftsIconCntr.setAttribute('style', 'width: 50px;min-height: 74px;display: block;cursor: pointer; ' +
'box-sizing: border-box;text-align: center;margin-left: -.3em;')
_draftsIconLink = document.createElement('a')
_draftsIconLink.setAttribute('id', 'draft_link')
_draftsIconLink.setAttribute('href', '/u/' + username + '#drafts')
_draftsIconLink.setAttribute('style', 'color: #666;padding: 10px 10px 0;display: block;min-height: 74px;position: relative;')
_draftsIconCount = document.createElement('span')
_draftsIconCount.setAttribute('class', 'num')
_draftsIconCount.setAttribute('style', 'display: block;position: absolute;' +
'border-radius: 50%;background: #dd4814;color: #fff;font-size:11px;width: 18px;height: 18px; line-height: 18px; font-family: sans-serif;' +
'top: 17px;left: 7px;z-index: 10;')
_draftsIconCount.textContent = draftsCount
_draftsIconImage = document.createElement('i')
_draftsIconImage.setAttribute('class', 'fa fa-file')
_draftsIconImage.setAttribute('style', 'font-size: 18px;color: #666;padding-top: 18px;')
_currentPointHeader = document.querySelector('.headerLinks span:nth-child(6)')
_currentPointHeader.parentNode.setAttribute('style', 'width:auto;')
_draftsIconLink.appendChild(_draftsIconCount)
_draftsIconLink.appendChild(_draftsIconImage)
_draftsIconCntr.appendChild(_draftsIconLink)
_currentPointHeader.parentNode.insertBefore(_draftsIconCntr, _currentPointHeader.nextSibling)
// Add Post Page
if (currentURI.indexOf('/add/post') > -1) {
_draftButtonSaveCntr = document.createElement('button')
_draftButtonSaveCntr.setAttribute('class', 'btn btn-primary')
_draftButtonSaveCntr.textContent = 'احفظ كمسودة'
_currentPointFormAdd = document.querySelector('#new_post .addBtns button')
_currentPointFormAdd.parentNode.insertBefore(_draftButtonSaveCntr, _currentPointFormAdd.nextSibling)
_draftButtonSaveCntr.addEventListener('click', saveDraft, true)
// Get data & set to inputs
if (currentURI.indexOf('#draft-') > -1) {
currentDraftKey = currentURIHash.split('-')[1]
draft = drafts[currentDraftKey]
if (draft) {
document.querySelector("#new_post input[name='post[title]']").value = draft.title
document.querySelector("#new_post textarea[name='post[content]']").value = draft.content
document.querySelector('#new_post #editable_post_content').textContent = draft.content
}
}
}
// Drafts tab in profile page
if (currentURI.indexOf('/u/' + username) > -1) {
_draftsTabCntr = document.createElement('li')
_draftsTabLink = document.createElement('a')
_draftsTabLink.setAttribute('href', '/u/' + username + '#drafts')
_draftsTabLink.textContent = 'مسوداتي'
_currentPointUserTabs = document.querySelector('.communitiesTabs .profile li:nth-child(2)')
_draftsTabCntr.appendChild(_draftsTabLink)
_currentPointUserTabs.parentNode.insertBefore(_draftsTabCntr, _currentPointUserTabs.nextSibling)
if (currentURI.indexOf('/u/' + username + '#drafts') > -1) {
document.querySelector('.tab-content .communityCardsBlocks').remove()
if (document.querySelector('.tab-content .clear')) {
document.querySelector('.tab-content .clear').remove()
}
if (document.querySelector('.tab-content #more_content')) {
document.querySelector('.tab-content #more_content').remove()
}
document.querySelector('.communitiesTabs .profile li:first-child').removeAttribute('class')
_draftsTabCntr.setAttribute('class', 'active')
_draftsCntr = document.createElement('div')
_draftsCntr.setAttribute('id', 'posts')
_draftsCntr.setAttribute('class', 'itemsList')
draftsReverse = Object.keys(drafts).sort().reverse();
for (var i = 0; i < Object.keys(drafts).length; i++) {
draftKey = draftsReverse[i]
draft = drafts[draftKey]
_draftCntr = document.createElement('div')
_draftCntr.setAttribute('id', 'draft-' + draftKey)
_draftCntr.setAttribute('class', 'listItem idthing')
_draftLink = document.createElement('a')
_draftLink.setAttribute('href', '/add/post#draft-' + draftKey)
_draftTitle = document.createElement('h2')
_draftTitle.setAttribute('style', 'line-height: 1.8;margin-top: 0;font-size: 18px;margin-bottom: 0;margin-right: 10px;')
_draftTitle.textContent = draft.title
_draftLink.appendChild(_draftTitle)
_draftCntr.appendChild(_draftLink)
_draftsCntr.appendChild(_draftCntr)
}
document.querySelector('.tab-content').appendChild(_draftsCntr)
}
}
}