Files

132 lines
3.6 KiB
JavaScript

// Bookmark Manipulation Utilities
// Handles bookmark operations for synchronization
(function() {
'use strict';
const BookmarkUtils = {
// Parse structured notes from Linkding bookmark
parseNotes(noteString) {
if (!noteString || typeof noteString !== 'string') {
return {
path: '',
userNotes: '',
autoTags: []
};
}
try {
return JSON.parse(noteString);
} catch (e) {
// If JSON parsing fails, treat as plain notes
return {
path: '',
userNotes: noteString,
autoTags: []
};
}
},
// Build structured notes object
buildNotes(path, userNotes, autoTags) {
return {
path: path || '',
userNotes: userNotes || '',
autoTags: autoTags || []
};
},
// Format notes for storage (always as JSON string)
formatNotes(notesObject) {
return JSON.stringify(notesObject);
},
// Extract folder path from bookmark
extractPath(bookmark) {
const notes = this.parseNotes(bookmark.notes || '');
return notes.path || '';
},
// Get display notes (userNotes only)
getDisplayNotes(bookmark) {
const notes = this.parseNotes(bookmark.notes || '');
return notes.userNotes || '';
},
// Get auto-generated tags
getAutoTags(bookmark) {
const notes = this.parseNotes(bookmark.notes || '');
return notes.autoTags || [];
},
// Create bookmark for Linkding API
createBookmarkData(url, title, description, notes, folder) {
return {
url: url,
title: title || '',
description: description || '',
notes: this.formatNotes(this.buildNotes(folder || '', notes || '')),
unread: false,
shared: false,
tag_names: []
};
},
// Update bookmark for Linkding API
updateBookmarkData(bookmarkId, title, description, notes, folder) {
return {
id: bookmarkId,
title: title || '',
description: description || '',
notes: this.formatNotes(this.buildNotes(folder || '', notes || '')),
unread: false,
shared: false
};
},
// Check if bookmark exists by URL
async checkExistingBookmark(url) {
const response = await fetch(`${window.linkdingSyncBaseURL}api/bookmarks/check/?url=${encodeURIComponent(url)}`, {
headers: {
'Authorization': `Token ${window.linkdingSyncApiKey}`
}
});
if (!response.ok) {
return null;
}
return await response.json();
},
// Merge bookmark data from browser and Linkding
mergeBookmarks(browserBookmark, linkdingBookmark) {
if (!linkdingBookmark) {
return browserBookmark;
}
// Keep browser's path, merge notes
const browserNotes = this.parseNotes(browserBookmark.notes || '');
const linkdingNotes = this.parseNotes(linkdingBookmark.notes || '');
// Use browser notes as primary
const mergedNotes = {
path: browserNotes.path || linkdingNotes.path || '',
userNotes: browserNotes.userNotes || linkdingNotes.userNotes || '',
autoTags: browserNotes.autoTags || linkdingNotes.autoTags || []
};
return {
...linkdingBookmark,
notes: this.formatNotes(mergedNotes),
title: browserBookmark.title || linkdingBookmark.title,
description: browserBookmark.description || linkdingBookmark.description
};
}
};
// Export for use in other scripts
window.BookmarkUtils = BookmarkUtils;
})();