|
| 1 | +var list = []; |
| 2 | + |
| 3 | +var status = ""; |
| 4 | + |
| 5 | +function startBrowsingCapture() { |
| 6 | + if (!browser.history.onVisited.hasListener(capture)) { |
| 7 | + browser.history.onVisited.addListener(capture); |
| 8 | + status = "STARTED"; |
| 9 | + console.log(Date().toLocaleString() + " starting"); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function stopBrowsingCapture() { |
| 14 | + if (browser.history.onVisited.hasListener(capture)) { |
| 15 | + browser.history.onVisited.removeListener(capture); |
| 16 | + status = "STOPPED"; |
| 17 | + console.log(Date().toLocaleString() + " stopping"); |
| 18 | + } |
| 19 | + |
| 20 | + downloadBrowsingHistory(); |
| 21 | +} |
| 22 | + |
| 23 | +function downloadBrowsingHistory() { |
| 24 | + |
| 25 | + if (list.length === 0) { |
| 26 | + console.log("empty browsing list - not saving anything !"); |
| 27 | + |
| 28 | + } else { |
| 29 | + saveData(list); |
| 30 | + console.log("browsing list saved"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +function capture(HistoryItem) { |
| 36 | + let dateISO = new Date(); |
| 37 | + console.log("Website opened"); |
| 38 | + console.log(dateISO.toISOString()); |
| 39 | + console.log(HistoryItem.url); |
| 40 | + console.log(HistoryItem.title); |
| 41 | + |
| 42 | + const temp = { |
| 43 | + date: dateISO.toISOString(), |
| 44 | + url: HistoryItem.url, |
| 45 | + title: HistoryItem.title |
| 46 | + } |
| 47 | + |
| 48 | + list.push(temp); |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +function getMessage() { |
| 53 | + if (!browser.runtime.onMessage.hasListener(processMessage)) { |
| 54 | + browser.runtime.onMessage.addListener(processMessage); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function stopMessage() { |
| 59 | + if (browser.runtime.onMessage.hasListener(processMessage)) { |
| 60 | + browser.runtime.onMessage.removeListener(processMessage); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function processMessage(data, sender) { |
| 65 | + console.log("MSG Copy " + data.content); |
| 66 | + |
| 67 | + if (data.content === '#start') { |
| 68 | + startBrowsingCapture(); |
| 69 | + return Promise.resolve({response: "done"}); |
| 70 | + } |
| 71 | + |
| 72 | + if (data.content === '#stop') { |
| 73 | + stopBrowsingCapture(); |
| 74 | + |
| 75 | + return Promise.resolve({response: "done"}); |
| 76 | + } |
| 77 | + |
| 78 | + if (data.content === '#status') { |
| 79 | + return Promise.resolve({response: status}); |
| 80 | + } |
| 81 | + |
| 82 | + if (data.content === '#download') { |
| 83 | + downloadBrowsingHistory(); |
| 84 | + return Promise.resolve({response: "done"}); |
| 85 | + } |
| 86 | + |
| 87 | + return Promise.resolve({response: "error"}); |
| 88 | +} |
| 89 | + |
| 90 | +function dateFile() { |
| 91 | + let dateUTC = new Date(); |
| 92 | + let dateString = dateUTC.getUTCFullYear() + "-" + (dateUTC.getUTCMonth() + 1) + "-" + dateUTC.getUTCDate() + "_" + |
| 93 | + dateUTC.getUTCHours() + "-" + dateUTC.getUTCMinutes() + "-" + dateUTC.getUTCSeconds(); |
| 94 | + return dateString; |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +function saveData(data) { |
| 99 | + let blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'}); |
| 100 | + let url = URL.createObjectURL(blob); |
| 101 | + let dateString = "regis-browser-history/" + dateFile() + ".txt"; |
| 102 | + let download = browser.downloads.download({url: url, filename: dateString, saveAs: false}); |
| 103 | + download.then(() => { |
| 104 | + list = [] |
| 105 | + }).catch(error => console.log(error)); |
| 106 | +} |
| 107 | + |
| 108 | +function init() { |
| 109 | + startBrowsingCapture(); |
| 110 | + getMessage(); |
| 111 | + |
| 112 | + browser.windows.onRemoved.addListener(() => { |
| 113 | + downloadBrowsingHistory() |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +init(); |
| 119 | + |
| 120 | + |
0 commit comments