Advertisement
Guest User

history-core.js

a guest
May 21st, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.51 KB | Source Code | 0 0
  1.  
  2. const history = {
  3.   getVisits(details) {
  4.     return new Promise(resolve => {
  5.       chrome.history.getVisits(details, function(visitItems) {
  6.         resolve(visitItems)
  7.       })
  8.     })
  9.   },
  10.  
  11.   search(query) {
  12.     return new Promise(resolve => {
  13.       chrome.history.search(query, function(historyItems) {
  14.         resolve(historyItems)
  15.       })
  16.     })
  17.   },
  18.  
  19.   unlimitedSearch(query) {
  20.     const now = (new Date).getTime()
  21.     Object.assign(query, {
  22.       endTime: now,
  23.       maxResults: 100,
  24.     })
  25.  
  26.     const data = {
  27.       visitItemsHash: {},
  28.       historyItems: [],
  29.     }
  30.  
  31.     function recursiveSearch(query) {
  32.       return history.search(query).then((historyItems) => {
  33.         historyItems = historyItems.filter(historyItem => {
  34.           if (data.visitItemsHash[historyItem.id]) {
  35.             return false
  36.           } else {
  37.             data.visitItemsHash[historyItem.id] = true
  38.             return true
  39.           }
  40.         })
  41.  
  42.         if (historyItems.length == 0) {
  43.           return data.visitItemsHash
  44.         } else {
  45.           const promises = []
  46.           for (let historyItem of historyItems) {
  47.             const details = {url: historyItem.url}
  48.             const promise = history.getVisits(details)
  49.             promises.push(promise)
  50.           }
  51.           return Promise.all(promises).then((allVisitItems) => {
  52.             let oldestLastVisitTime = now
  53.  
  54.             for (var i = 0; i < historyItems.length; i++) {
  55.               const historyItem = historyItems[i]
  56.               const visitItems = allVisitItems[i]
  57.               data.visitItemsHash[historyItem.id] = visitItems
  58.  
  59.               for (visitItem of visitItems) {
  60.                 visitItem.title = ''
  61.                 Object.assign(visitItem, historyItem)
  62.               }
  63.  
  64.               if (oldestLastVisitTime > historyItem.lastVisitTime) {
  65.                 oldestLastVisitTime = historyItem.lastVisitTime
  66.               }
  67.             }
  68.  
  69.             query.endTime = oldestLastVisitTime
  70.             return recursiveSearch(query)
  71.           })
  72.         }
  73.       })
  74.     }
  75.  
  76.     return recursiveSearch(query).then((visitItemsHash) => {
  77.       let allVisitItems = []
  78.       for (visitItems of Object.keys(visitItemsHash)) {
  79.         allVisitItems = allVisitItems.concat(visitItemsHash[visitItems])
  80.       }
  81.       allVisitItems.sort((a, b) => {
  82.         return b.visitTime - a.visitTime
  83.       })
  84.       allVisitItems = allVisitItems.filter(a => {
  85.         return a.visitTime > query.startTime
  86.       })
  87.       return allVisitItems
  88.     })
  89.   }
  90. }
  91.  
  92. function downloadJson(historyItems) {
  93.   const historyItemsString = JSON.stringify(historyItems, null, 2)
  94.   const blob = new Blob([historyItemsString], {type: 'application/json'})
  95.   const url = URL.createObjectURL(blob)
  96.   const filename = 'history.json'
  97.   chrome.downloads.download({
  98.     filename,
  99.     url,
  100.   })
  101. }
  102.  
  103. function csvDate(date) {
  104.   return `${date.getMonth()+1}/${date.getDate()}/${date.getFullYear()}`
  105. }
  106.  
  107. function csvTime(date) {
  108.   const minutes = date.getMinutes()
  109.   const minutesString = minutes < 10 ? '0' + minutes : '' + minutes
  110.   const seconds = date.getSeconds()
  111.   const secondsString = seconds < 10 ? '0' + seconds : '' + seconds
  112.   return `${date.getHours()}:${minutesString}:${secondsString}`
  113. }
  114.  
  115. function csvEscapify(str) {
  116.   const escapeChars = [',', '"', '\r', '\n']
  117.   let needsEscaping = false
  118.   for (let escapeChar of escapeChars) {
  119.     needsEscaping = needsEscaping || str.indexOf(escapeChar) > -1
  120.   }
  121.   if (!needsEscaping) return str
  122.   return `"${str.replace(/"/g, '""')}"`
  123. }
  124.  
  125. function downloadCsv(historyItems) {
  126.  let raw = '\ufeff'
  127.  raw += 'order,id,date,time,title,url,visitCount,typedCount,transition\r\n'
  128.  
  129.  for (let i = 0; i < historyItems.length; i++) {
  130.    historyItem = historyItems[i]
  131.  
  132.    const order = i
  133.    const {id, visitCount, typedCount, transition} = historyItem
  134.    const visitTime = new Date(historyItem.visitTime)
  135.    const date = csvDate(visitTime)
  136.    const time = csvTime(visitTime)
  137.    const title = csvEscapify(historyItem.title)
  138.    const url = csvEscapify(historyItem.url)
  139.  
  140.    const entry = [
  141.      order,
  142.      id,
  143.      date,
  144.      time,
  145.      title,
  146.      url,
  147.      visitCount,
  148.      typedCount,
  149.      transition,
  150.    ]
  151.  
  152.    const entryString = entry.join(',')
  153.    raw += entryString + '\r\n'
  154.  }
  155.  
  156.  const blob = new Blob([raw], {type: 'text/csv;charset=UTF-16LE;'})
  157.  const url = URL.createObjectURL(blob)
  158.  const filename = 'history.csv'
  159.  chrome.downloads.download({
  160.    filename,
  161.    url,
  162.  })
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement