-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbookmarks.html
263 lines (258 loc) · 8.02 KB
/
bookmarks.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="author" content="M A Eyler, Istanbul, 2020" />
<link rel="icon" href="image/icon.png">
<title>Yer işaretleri</title>
<style>
body {
max-width: 540px;
background: #fff8d0;
}
p { font: 11pt/1.3 sans-serif; }
button { cursor: pointer; }
img {
position: fixed;
top:8px; right:22px;
width: 50px;
}
pre {
overflow-x: auto;
line-height: 1.3;
}
input { width: 97%; }
#localB, #driveB {
background: #bcf;
margin-bottom: 5px;
font-size: 16px;
}
.mavi { color: blue; }
.large { font-size: 24px; }
</style>
</head>
<body>
<h2><span id=title></span>   <button id=backbutton>◀</button></h2>
<img id=image>
<p>
Bu cihazda yıldızla işaretlediğiniz sayfaları,
kullandığınız diğer cihazlarda da yıldızlı görmek istiyorsanız,
Google Drive hesabınızdan erişim izni vermeniz gerekiyor.
İstenen yetki, bu yazılıma ait olan "iqra bookmarks" klasörüne
erişmekten ibarettir, başka Drive dosyalarına erişim izni istenmiyor.
</p><p>
İşaretleri kullanmak için iki yöntem var: <br>
* Buluttaki yer işaretlerini cihaza aktar. <br>
* Cihazdaki yer işaretlerini buluta aktar. <br>
Kullanıcı listeleri dilediği gibi düzenler ve "Aktar" düğmelerinden
birine basar. Sayıları değiştirmediyse yerel ve bulut işaretleri
eşitlenecektir. Yazılım en çok 12 adet yer işareti kaydedebilir.
</p><p>
<button id=auth onclick="doSignIn()"
title="Bulut erişimi için yetkilendirme">Giriş</button>
<span id=user title="Yetki veren kullanıcı"></span> 
<!-- <input type=checkbox id=auto onchange="autoSync()"
title="Seçili ise, yer işaretleri otomatik eşitlenir">
<label for=auto>Otomatik eşitle</label> -->
</p><p>
Buluttaki yer işaretleri
<button id=driveB disabled onclick="useDriveMarks()"
title="Buluttan indirirken cihazdaki eski işaretler silinir">
Cihaza aktar</button>
<span class=large>⇓</span><br>
<input type=text inputmode=numeric id=driveT class=mavi
title="Bulutta (Google Drive) saklanan yer işaretleri"></input>
</p><p>
</p><p>
Cihazdaki yer işaretleri
<button id=localB disabled onclick="useLocalMarks()"
title="Buluta kaydederken oradaki eski işaretler silinir">
Buluta aktar</button>
<span class=large>⇑</span><br>
<input type=text inputmode=numeric id=localT class=mavi
title="Cihazda (Yerel kayıt) saklanan yer işaretleri"></input>
<!-- </p><p>
Seçilen işaretler:  
<button id=sync disabled onclick="useInput()"
title="Seçilen işaretleri buluta ve cihaza kaydet">Eşitle</button>
<span class=large>⇑⇑</span><br>
<input type=text inputmode=numeric id=input
title="Seçilen işaretleri dilediğiniz gibi düzenleyebilirsiniz"> -->
</p>
<pre id=out></pre>
<script src="code/common.js"></script>
<script src="https://apis.google.com/js/api.js"></script>
<script>
"use strict";
/**
* https://developers.google.com/explorer-help/guides/code_samples
*/
function stringify(m) {
if (typeof m === "string") return m
return JSON.stringify(m,'',2)
}
function log(m) {
let r = m.result || m
console.log(m); out.style.color = ''
out.innerText = stringify(r)
}
function error(e) {
let r = e.error || e.result || e
console.error(e); out.style.color = 'red'
out.innerText = stringify(r)
}
async function doSignIn() {
log("Authorization");
try {
await gapi.auth2.getAuthInstance().signIn()
displayMarks()
} catch (e) { error(e) }
}
async function loadClient() {
const DRIVE =
"https://content.googleapis.com/discovery/v1/apis/drive/v3/rest"
if (!gapi.client.drive) try {
log("Load drive client");
await gapi.client.load(DRIVE)
log("GAPI client loaded")
} catch (e) { error(e) }
}
async function doCreate(name, mimeType, parentId) {
log("create "+name);
try {
let resource = {name, mimeType}
if (parentId) resource.parents = [parentId]
return gapi.client.drive.files.create(resource)
} catch (e) { error(e) }
}
async function getFileData(fileId, fields) {
log("getFileData "+fileId);
try {
return gapi.client.drive.files.get({fileId, fields})
} catch (e) { error(e) }
}
async function doSearch(q, fields) {
log("search ["+q+"]");
try {
return gapi.client.drive.files.list({q, fields})
} catch (e) { error(e) }
}
async function doRename(fileId, name) {
log("rename "+name)
try {
let resource = { name }
return gapi.client.drive.files.update({fileId, resource})
} catch (e) { error(e) }
}
function useDriveMarks() {
let a = useInput(driveT)
//if opened by Iqra, opener.name == 'iqra' -- use a
if (opener && opener.arrayToSet) opener.arrayToSet(a)
setStorage('iqra', 'marks', a) //save a to localStorage
localT.value = localT.old = a.join(' ');
driveT.value = driveT.old;
parent.iqra.readSettings()
log("Cihaza aktarıldı")
}
async function useLocalMarks() {
let s = useInput(localT).join(' ')
if (!marksId) await getMarksId()
await doRename(marksId, s) //save s to Drive
driveT.value = driveT.old = s;
localT.value = localT.old;
log("Buluta aktarıldı")
}
function useInput(elt) {
let s = elt.value.trim()
if (!s) {
s = 'en az bir yer işareti gerekiyor'
elt.value = elt.old; error(s); throw s
}
let a = s.split(/\D+/)
ensureMaxSize(a); return a
}
const MAX_ITEMS = 6 //items from each list
function ensureMaxSize(a) {
let d = a.length - 2*MAX_ITEMS
if (d > 0) a.splice(0, d)
}
function autoSync() {
if (auto.checked) {
setStorage('iqra', 'marksId', marksId)
} else {
setStorage('iqra', 'marksId', undefined)
}
}
function currentUser() {
return gapi.auth2.getAuthInstance().currentUser.get()
}
function showProfile() {
let p = currentUser().getBasicProfile()
image.src = p.getImageUrl()
image.title = p.getName()+'\n'+p.getEmail()
user.innerText = p.getEmail()
}
var folderId, marksId
const FOLDER = 'application/vnd.google-apps.folder',
M_FOLDER = 'iqra bookmarks', M_MIME = 'text/iqra',
apiKey = 'AIzaSyCH22pYgwYoWme6CFPiU3i_JLD-SgATteU',
clientId = "864962883135-ut9cl3g1jlqv1frlp2p6cq9r3jsfflkv.apps.googleusercontent.com",
scope = 'https://www.googleapis.com/auth/drive.file';
async function init() {
try {
await gapi.auth2.init({ apiKey, clientId, scope })
await loadClient();
if (!currentUser().getId()) return
marksId = getStorage('iqra', 'marksId'); displayMarks()
// auto.checked = marksId? true : false
} catch (e) { error(e) }
}
async function getMarksId() {
log('getMarksId')
let fields = 'files(id,name,mimeType,parents)'
{
let q = 'mimeType = "'+FOLDER+'" and name = "'+M_FOLDER+'"'
let {result} = await doSearch(q, fields)
let files = result.files
if (files.length) folderId = files[0].id
else {
let {result} = await doCreate(M_FOLDER, FOLDER)
log(result.id); folderId = result.id
}
}
let q = 'trashed = false and mimeType contains "iqra"'
let {result} = await doSearch(q, fields)
let files = result.files
if (files.length) marksId = files[0].id
else {
let {result} = await doCreate('no_name', M_MIME, folderId)
log(result.id); marksId = result.id
}
return new Promise((resolve, reject) => resolve())
}
async function displayMarks() {
showProfile(); //sync.disabled = false
localB.disabled = false; driveB.disabled = false
let x = getStorage('iqra') || {}
//local marks a
let a = x.marks
localT.value = localT.old = (a && a.join(' ')) || '(yok)'
//drive data b
marksId = x.marksId
if (!marksId) await getMarksId()
let str, b;
if (marksId) {
let {result} = await getFileData(marksId)
console.log(result); str = result.name; b = str.split(' ')
} else {
str = '(yok)'; b = []
}
driveT.value = driveT.old = str
}
title.innerText = document.title
gapi.load("client:auth2", init);
backbutton.onclick = () => {location.pathname = "/Kuran/start.html"}
</script>
</body>
</html>