-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.js
More file actions
454 lines (388 loc) · 11.9 KB
/
Copy pathshared.js
File metadata and controls
454 lines (388 loc) · 11.9 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// 共通ユーティリティ関数
// URLを正規化(プロトコル、www、末尾スラッシュを削除)
function normalizeUrl(url) {
if (!url) return '';
try {
// プロトコルがない場合はhttpsを追加
let normalizedUrl = url;
if (!url.includes('://')) {
normalizedUrl = 'https://' + url;
}
const urlObj = new URL(normalizedUrl);
let normalized = urlObj.hostname;
// www.を削除
if (normalized.startsWith('www.')) {
normalized = normalized.substring(4);
}
// パスを追加(ルートでない場合)
if (urlObj.pathname !== '/') {
normalized += urlObj.pathname;
}
// 末尾のスラッシュを削除
if (normalized.endsWith('/')) {
normalized = normalized.slice(0, -1);
}
return normalized;
} catch (error) {
console.error('URL正規化エラー:', error);
return url;
}
}
// 一意なIDを生成
function generateUniqueId() {
const timestamp = Date.now().toString(36);
const randomStr = Math.random().toString(36).substring(2, 8);
return `${timestamp}-${randomStr}`;
}
// URLの検証
function validateUrl(url) {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
}
// 日付をフォーマット
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleDateString('ja-JP', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
}
// HTMLエスケープ
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 指定URLのJumpmarksを取得
async function getJumpmarksForUrl(url) {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const allJumpmarks = result.jumpmarks || {};
return allJumpmarks[url] || [];
} catch (error) {
console.error('Jumpmarks取得エラー:', error);
return [];
}
}
// 全てのJumpmarksを取得
async function getAllJumpmarks() {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const allJumpmarks = result.jumpmarks || {};
// URLごとのJumpmarksを平坦化
const jumpmarksList = [];
Object.entries(allJumpmarks).forEach(([sourceUrl, jumpmarks]) => {
jumpmarks.forEach(jumpmark => {
jumpmarksList.push({
...jumpmark,
sourceUrl: sourceUrl
});
});
});
return jumpmarksList;
} catch (error) {
console.error('全Jumpmarks取得エラー:', error);
return [];
}
}
// Jumpmarkを保存
async function saveJumpmark(jumpmarkData) {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const jumpmarks = result.jumpmarks || {};
const sourceUrl = normalizeUrl(jumpmarkData.sourceUrl);
const targetUrl = jumpmarkData.url;
// 新しいJumpmarkを作成
const newJumpmark = {
id: generateUniqueId(),
title: jumpmarkData.title,
url: targetUrl,
icon: jumpmarkData.icon || '🔗',
sourceUrl: sourceUrl,
created: new Date().toISOString()
};
// ソースURLのJumpmarksに追加
if (!jumpmarks[sourceUrl]) {
jumpmarks[sourceUrl] = [];
}
jumpmarks[sourceUrl].push(newJumpmark);
// 双方向リンクの場合、逆方向も作成
if (jumpmarkData.createBidirectional) {
const normalizedTargetUrl = normalizeUrl(targetUrl);
if (!jumpmarks[normalizedTargetUrl]) {
jumpmarks[normalizedTargetUrl] = [];
}
// 逆方向のJumpmarkを作成
const reverseJumpmark = {
id: generateUniqueId(),
title: jumpmarkData.reverseTitle || `← ${jumpmarkData.title}`,
url: jumpmarkData.sourceUrl,
icon: jumpmarkData.icon || '🔗',
sourceUrl: normalizedTargetUrl,
created: new Date().toISOString()
};
jumpmarks[normalizedTargetUrl].push(reverseJumpmark);
}
await chrome.storage.sync.set({ jumpmarks });
return newJumpmark;
} catch (error) {
console.error('Jumpmark保存エラー:', error);
throw error;
}
}
// Jumpmarkを更新(シンプル版)
async function updateJumpmark(jumpmarkId, updateData) {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const jumpmarks = result.jumpmarks || {};
let found = false;
// Jumpmarkを検索して更新
Object.entries(jumpmarks).forEach(([url, jumpmarkList]) => {
const index = jumpmarkList.findIndex(j => j.id === jumpmarkId);
if (index !== -1) {
// sourceUrlも更新データに含まれている場合は更新
const updatedJumpmark = {
...jumpmarkList[index],
...updateData,
sourceUrl: updateData.sourceUrl || jumpmarkList[index].sourceUrl
};
jumpmarkList[index] = updatedJumpmark;
found = true;
}
});
if (!found) {
throw new Error('Jumpmarkが見つかりません');
}
await chrome.storage.sync.set({ jumpmarks });
return true;
} catch (error) {
console.error('Jumpmark更新エラー:', error);
throw error;
}
}
// Jumpmarkを削除(単体)
async function deleteJumpmark(jumpmarkId) {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const jumpmarks = result.jumpmarks || {};
let found = false;
// Jumpmarkを検索して削除
Object.entries(jumpmarks).forEach(([url, jumpmarkList]) => {
const index = jumpmarkList.findIndex(j => j.id === jumpmarkId);
if (index !== -1) {
jumpmarkList.splice(index, 1);
found = true;
// 空になったURLエントリを削除
if (jumpmarkList.length === 0) {
delete jumpmarks[url];
}
}
});
if (!found) {
throw new Error('Jumpmarkが見つかりません');
}
await chrome.storage.sync.set({ jumpmarks });
return true;
} catch (error) {
console.error('Jumpmark削除エラー:', error);
throw error;
}
}
// 双方向ペアを削除
async function deleteBidirectionalPair(jumpmarkId, partnerId) {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const jumpmarks = result.jumpmarks || {};
let deletedCount = 0;
// 両方のJumpmarkを削除
[jumpmarkId, partnerId].forEach(id => {
Object.entries(jumpmarks).forEach(([url, jumpmarkList]) => {
const index = jumpmarkList.findIndex(j => j.id === id);
if (index !== -1) {
jumpmarkList.splice(index, 1);
deletedCount++;
// 空になったURLエントリを削除
if (jumpmarkList.length === 0) {
delete jumpmarks[url];
}
}
});
});
await chrome.storage.sync.set({ jumpmarks });
return deletedCount;
} catch (error) {
console.error('双方向ペア削除エラー:', error);
throw error;
}
}
// 複数のJumpmarksを削除
async function deleteJumpmarks(jumpmarkIds) {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const jumpmarks = result.jumpmarks || {};
let deletedCount = 0;
// 各Jumpmarkを削除
jumpmarkIds.forEach(jumpmarkId => {
Object.entries(jumpmarks).forEach(([url, jumpmarkList]) => {
const index = jumpmarkList.findIndex(j => j.id === jumpmarkId);
if (index !== -1) {
jumpmarkList.splice(index, 1);
deletedCount++;
// 空になったURLエントリを削除
if (jumpmarkList.length === 0) {
delete jumpmarks[url];
}
}
});
});
await chrome.storage.sync.set({ jumpmarks });
return deletedCount;
} catch (error) {
console.error('複数Jumpmark削除エラー:', error);
throw error;
}
}
// ストレージ統計を取得
async function getStorageStats() {
try {
const result = await chrome.storage.sync.get(['jumpmarks']);
const jumpmarks = result.jumpmarks || {};
let totalJumpmarks = 0;
let originalJumpmarks = 0;
let bidirectionalJumpmarks = 0;
let urlCount = 0;
Object.entries(jumpmarks).forEach(([url, jumpmarkList]) => {
if (jumpmarkList.length > 0) {
urlCount++;
totalJumpmarks += jumpmarkList.length;
jumpmarkList.forEach(jumpmark => {
if (jumpmark.bidirectional) {
bidirectionalJumpmarks++;
} else {
originalJumpmarks++;
}
});
}
});
// ストレージ使用量を計算(概算)
const dataSize = JSON.stringify(jumpmarks).length;
const storageUsed = Math.round(dataSize / 1024 * 100) / 100; // KB単位
return {
totalJumpmarks,
originalJumpmarks,
bidirectionalJumpmarks,
urlCount,
storageUsed
};
} catch (error) {
console.error('ストレージ統計取得エラー:', error);
return {
totalJumpmarks: 0,
originalJumpmarks: 0,
bidirectionalJumpmarks: 0,
urlCount: 0,
storageUsed: 0
};
}
}
// デバウンス関数
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// イベントエミッター
class EventEmitter {
constructor() {
this.events = {};
}
on(event, callback) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(callback);
}
off(event, callback) {
if (this.events[event]) {
this.events[event] = this.events[event].filter(cb => cb !== callback);
}
}
emit(event, data) {
if (this.events[event]) {
this.events[event].forEach(callback => callback(data));
}
}
}
// グローバルイベントエミッター
const eventEmitter = new EventEmitter();
// 拡張機能のバージョンを取得
function getExtensionVersion() {
return chrome.runtime.getManifest().version;
}
// 現在のタブ情報を取得
async function getCurrentTab() {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab;
} catch (error) {
console.error('タブ情報取得エラー:', error);
return null;
}
}
// 双方向リンクのペア判定
function isBidirectionalPair(jumpmarkA, jumpmarkB) {
if (!jumpmarkA || !jumpmarkB || jumpmarkA.id === jumpmarkB.id) {
return false;
}
const urlA = normalizeUrl(jumpmarkA.url);
const urlB = normalizeUrl(jumpmarkB.url);
const sourceA = jumpmarkA.sourceUrl;
const sourceB = jumpmarkB.sourceUrl;
return urlA === sourceB && urlB === sourceA;
}
// 指定jumpmarkと対になる双方向リンクを検索
async function findBidirectionalPartner(targetJumpmark) {
try {
const allJumpmarks = await getAllJumpmarks();
return allJumpmarks.find(jm => isBidirectionalPair(targetJumpmark, jm));
} catch (error) {
console.error('双方向パートナー検索エラー:', error);
return null;
}
}
// URLに移動(同一URLのタブがあればフォーカス、なければ新タブ作成)
async function navigateToUrl(url) {
try {
// 全てのタブを取得
const tabs = await chrome.tabs.query({});
// 完全に同じURLのタブを探す
const exactTab = tabs.find(tab => tab.url === url);
if (exactTab) {
// 同じURLのタブがある場合:フォーカスのみ(リロードしない)
await chrome.tabs.update(exactTab.id, { active: true });
await chrome.windows.update(exactTab.windowId, { focused: true });
} else {
// 同じURLのタブがない場合:新しいタブを作成
await chrome.tabs.create({ url: url });
}
// ポップアップを閉じる
if (window.close) {
window.close();
}
} catch (error) {
console.error('URL移動エラー:', error);
}
}