bookmarks.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const fileHash = require('../../util/backup_filehash')
  2. const log = require('../../util/log')
  3. const BOOKMARKS_DB = fileHash('Library/Safari/Bookmarks.db')
  4. module.exports = {
  5. version: 4,
  6. name: 'safari.bookmarks',
  7. description: `List all Safari bookmarks`,
  8. requiresBackup: true,
  9. // Run on a v3 lib / backup object
  10. run (lib, { backup }) {
  11. return getAllBookmarks(backup)
  12. },
  13. // Available fields.
  14. output: {
  15. id: el => el.id,
  16. title: el => el.title ? el.title.trim() : '',
  17. url: el => el.url ? el.url.trim() : '',
  18. parent: el => el.parent_title
  19. }
  20. }
  21. function getAllBookmarks (backup) {
  22. return new Promise(async (resolve, reject) => {
  23. // Try to run newer version. If it fails, try older.
  24. try {
  25. let iOSBookmarks = await getSafariBookmarksLater(backup)
  26. return resolve(iOSBookmarks)
  27. } catch (e) {
  28. log.verbose(`Couldn't load iOS7+ bookmarks`, e)
  29. }
  30. // Run older.
  31. try {
  32. let iOS7Bookmarks = await getSafariBookmarksiOS7(backup)
  33. return resolve(iOS7Bookmarks)
  34. } catch (e) {
  35. log.verbose(`Couldn't load iOS7+ bookmarks`, e)
  36. }
  37. // Fail
  38. reject(new Error('Could not find any bookmarks. Use -v to see error info.'))
  39. })
  40. }
  41. // Get iOS Bookmarks for newer versions
  42. function getSafariBookmarksLater (backup) {
  43. return new Promise((resolve, reject) => {
  44. backup.openDatabase(BOOKMARKS_DB)
  45. .then(db => {
  46. const query = `
  47. select bookmarks.id
  48. , bookmarks.title
  49. , bookmarks.url
  50. , bookmarks.parent as parent_id
  51. , bookmarks.special_id
  52. , bookmarks.type
  53. , bookmarks.num_children
  54. , bookmarks.editable
  55. , bookmarks.deletable
  56. , bookmarks.hidden
  57. , bookmarks.hidden_ancestor_count
  58. , bookmarks.order_index
  59. , bookmarks.external_uuid
  60. , bookmarks.read
  61. , bookmarks.last_modified
  62. , bookmarks.server_id
  63. , bookmarks.sync_key
  64. , bookmarks.added
  65. , bookmarks.deleted
  66. , bookmarks.fetched_icon
  67. , bookmarks.dav_generation
  68. , bookmarks.locally_added
  69. , bookmarks.archive_status
  70. , bookmarks.syncable
  71. , bookmarks.web_filter_status
  72. , bookmarks.modified_attributes
  73. , parent_bookmarks.title as parent_title
  74. from bookmarks
  75. left join bookmarks as parent_bookmarks on parent_bookmarks.id = bookmarks.parent
  76. where bookmarks.type = 0
  77. order by bookmarks.id
  78. `
  79. db.all(query, async function (err, rows) {
  80. if (err) reject(err)
  81. resolve(rows)
  82. })
  83. })
  84. .catch(reject)
  85. })
  86. }
  87. // Run older bookmark report
  88. function getSafariBookmarksiOS7 (backup) {
  89. return new Promise((resolve, reject) => {
  90. backup.openDatabase(BOOKMARKS_DB)
  91. .then(db => {
  92. const query = `
  93. select bookmarks.id
  94. , bookmarks.special_id
  95. , bookmarks.parent as parent_id
  96. , bookmarks.type
  97. , bookmarks.title
  98. , bookmarks.url
  99. , bookmarks.num_children
  100. , bookmarks.editable
  101. , bookmarks.deletable
  102. , bookmarks.hidden
  103. , bookmarks.hidden_ancestor_count
  104. , bookmarks.order_index
  105. , bookmarks.external_uuid
  106. , bookmarks.read
  107. , bookmarks.last_modified
  108. , bookmarks.server_id
  109. , bookmarks.sync_key
  110. , bookmarks.sync_data
  111. , bookmarks.added
  112. , bookmarks.deleted
  113. , bookmarks.extra_attributes
  114. , bookmarks.local_attributes
  115. , bookmarks.fetched_icon
  116. , bookmarks.icon
  117. , bookmarks.dav_generation
  118. , bookmarks.locally_added
  119. , bookmarks.archive_status
  120. , bookmarks.syncable
  121. , bookmarks.web_filter_status
  122. , parent_bookmarks.title as parent_title
  123. from bookmarks
  124. left join bookmarks as parent_bookmarks on parent_bookmarks.id = bookmarks.parent
  125. where bookmarks.type = 0
  126. order by bookmarks.id
  127. `
  128. db.all(query, async function (err, rows) {
  129. if (err) reject(err)
  130. resolve(rows)
  131. })
  132. })
  133. .catch(reject)
  134. })
  135. }