Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. function HistoryVisitFinder() {
  2.  
  3. /* Largest value of `maxResults` property that is accepted by the `query`
  4. argument of browser.history.search().
  5. This is a temporary workaround to the lack of a method for streaming large
  6. number of results. */
  7. const maxResultsCeiling = Math.pow(2, 52);
  8. const msPerDay = 86400000;
  9.  
  10. function getLocalDateMsBounds(date) {
  11. /* Get the start and end time in milliseconds-since-epoch for the local day
  12. of `date` */
  13.  
  14. const localDateStart = (new Date(date.getFullYear(), date.getMonth(),
  15. date.getDate())).getTime();
  16. return [localDateStart, localDateStart + msPerDay - 1];
  17. }
  18.  
  19. this.searchVisits = async function (text, startTime, endTime) {
  20. /*
  21. Find objects representing webpage history visits whose domain and/or url
  22. contain `text` and whose visit times are within the inclusive range of
  23. `startTime` to `endTime`.
  24.  
  25. Returns an array of objects with the associated HistoryItem and VisitItem
  26. attributes:
  27. { url, title, id, referringVisitId, transition, visitTime }
  28. */
  29. const results = [];
  30. const query = { text, startTime, endTime, maxResults: maxResultsCeiling };
  31. const historyItemArray = await browser.history.search(query);
  32. const expected = [];
  33.  
  34. const filterVisitsCallback = function (visit) {
  35. return visit.visitTime <= endTime && visit.visitTime >= startTime;
  36. }
  37.  
  38. /* Store the visits of each HistoryItem that fall between `startTime` and
  39. `endTime` */
  40. for (let historyItem of historyItemArray) {
  41. const url = historyItem.url;
  42. const title = historyItem.title;
  43.  
  44. const mapVisitsCallback = function (visit) {
  45. return {
  46. url, title,
  47. id: visit.visitId,
  48. referringVisitId: visit.referringVisitId,
  49. transition: visit.transition,
  50. visitTime: visit.visitTime
  51. }
  52. }
  53.  
  54. expected.push(browser.history.getVisits({ url })
  55. .then(visitItemArray => {
  56. results.push(...(
  57. visitItemArray
  58. .filter(filterVisitsCallback)
  59. .map(mapVisitsCallback)
  60. ))
  61. })
  62. .catch(reason => {
  63. console.warn('Could not get visits for HistoryItem', historyItem,
  64. 'Reason:', reason);
  65. })
  66. );
  67. }
  68.  
  69. return await Promise.all(expected).then(() => {
  70. /* Sort results in reverse chronological order */
  71. results.sort((a, b) => b.visitTime - a.visitTime);
  72. return results;
  73. });
  74. }
  75.  
  76. this.getVisitsOnDate = async function (text, date) {
  77. /*
  78. Find objects representing webpage history visits whose domain and/or url
  79. contain `text` and whose visit times are on the same day as `date`.
  80.  
  81. Returns an array of objects with the associated HistoryItem and VisitItem
  82. attributes:
  83. { url, title, id, referringVisitId, transition, visitTime }
  84. */
  85. const [startTime, endTime] = getLocalDateMsBounds(date);
  86.  
  87. return await this.searchVisits(text, startTime, endTime);
  88.  
  89. }
  90. }
  91.  
  92. const historyFinder = HistoryVisitFinder();
  93. historyFinder.getVisitsOnDate('', new Date()).then(console.log);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement