Skip to content

Commit

Permalink
v0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
acendrou committed Jan 14, 2022
1 parent f3de898 commit 5a783b0
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 0 deletions.
294 changes: 294 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
var list = [];

var status = "";

function startBrowsingCapture() {
statusCreation = false;
statusRemoval = false;
statusChanged = false;
statusMoved = false;

// CREATION
if (!browser.bookmarks.onCreated.hasListener(captureCreation)) {
browser.bookmarks.onCreated.addListener(captureCreation);
statusCreation = true;
console.log(Date().toLocaleString() + " starting: created log");
}

// REMOVAL
if (!browser.bookmarks.onRemoved.hasListener(captureRemoval)) {
browser.bookmarks.onRemoved.addListener(captureRemoval);
statusRemoval = true;
console.log(Date().toLocaleString() + " starting: removal log");
}

// CHANGED
if (!browser.bookmarks.onChanged.hasListener(captureChanged)) {
browser.bookmarks.onChanged.addListener(captureChanged);
statusChanged = true;
console.log(Date().toLocaleString() + " starting: changed log");
}

// MOVED
if (!browser.bookmarks.onMoved.hasListener(captureMoved)) {
browser.bookmarks.onMoved.addListener(captureMoved);
statusMoved = true;
console.log(Date().toLocaleString() + " starting: changed log");
}


if (statusCreation == true && statusRemoval == true && statusChanged == true && statusMoved == true)
{
status = "STARTED";
}else
{
stopBrowsingCapture();
}

}

function stopBrowsingCapture() {

// CREATION
if (browser.bookmarks.onCreated.hasListener(captureCreation)) {
browser.bookmarks.onCreated.removeListener(captureCreation);
status = "STOPPED";
console.log(Date().toLocaleString() + " stopping: created log");
}

// REMOVAL
if (browser.bookmarks.onRemoved.hasListener(captureRemoval)) {
browser.bookmarks.onRemoved.removeListener(captureRemoval);
status = "STOPPED";
console.log(Date().toLocaleString() + " stopping: removal log");
}

// CHANGED
if (browser.bookmarks.onChanged.hasListener(captureChanged)) {
browser.bookmarks.onChanged.removeListener(captureChanged);
status = "STOPPED";
console.log(Date().toLocaleString() + " stopping: changed log");
}

// MOVED
if (browser.bookmarks.onMoved.hasListener(captureMoved)) {
browser.bookmarks.onMoved.removeListener(captureMoved);
status = "STOPPED";
console.log(Date().toLocaleString() + " stopping: moved log");
}


downloadBrowsingBookmark();
}

function downloadBrowsingBookmark() {

if (list.length === 0) {
console.log("empty browsing list - not saving anything !");

} else {
saveData(list);
console.log("browsing list saved");
}
}


function captureCreation(id, bookmarkInfo) {
console.log("Bookmark created");

if (bookmarkInfo.type === "bookmark")
{
logInfo(bookmarkInfo, id, "creation");

}else if (bookmarkInfo.type === "folder") {
// don't log when a folder is created because on Firefox, it is weird behavior :
// the folder is always called " new folder" and not by the name the user choose

/* const temp = writeInfo(bookmarkInfo, id, "creation");
list.push(temp); */
console.log("FOLDER: DO NOTHING");
}else
{
console.log("SEPARATOR: DO NOTHING");
}
}

function captureRemoval(id, removeInfo){
console.log("Bookmark removed");

if (removeInfo.node.type === "bookmark")
{
logInfo(removeInfo.node, id, "removal");

}else if (removeInfo.node.type === "folder") {

logInfo(removeInfo.node, id,"removal");
}else
{
console.log("SEPARATOR: DO NOTHING");
}
}


function captureChanged (id, changeInfo){
console.log("Bookmark changed");

logMoreInfo(id,"change");

// const temp = writeInfo(changeInfo,id, "change");
// list.push(temp);
}

function captureMoved(id, moveInfo){
console.log("Bookmark moved");

// logMoreInfo(id,"move");

getBookmarkMoveInfos(id, moveInfo.parentId, moveInfo.oldParentId)
.then((bookmarks) => {
let temp = {
move: bookmarks[0],
from: bookmarks[1],
to: bookmarks[2]
}

list.push(temp);
});

}

async function getBookmarkMoveInfos(id, parentId, oldParentId) {
let bookmarkInfo;
bookmarkInfo = await getMoreInfo(id);
let bookmarkMoved = writeInfo(bookmarkInfo, id, "move");

bookmarkInfo = await getMoreInfo(oldParentId);
let bookmarkOldParentMoved = writeInfo(bookmarkInfo, oldParentId, "move");

bookmarkInfo = await getMoreInfo(parentId);
let bookmarkParentMoved = writeInfo(bookmarkInfo, parentId, "move");

return [bookmarkMoved, bookmarkOldParentMoved, bookmarkParentMoved];
}

function logMoreInfo(id, action) {
let bookmarkInfo;
console.log("More info for: " + id);
let gettingBookmarks = browser.bookmarks.get(id);
gettingBookmarks.then(onFulfilledGetBookmark, error => (console.log("error get bookmark: " + error)))
.then(bookmarkInfo => {logInfo(bookmarkInfo[0], id, action);});
}

function getMoreInfo(id) {
let bookmarkInfo;
console.log("More info for: " + id);
let gettingBookmarks = browser.bookmarks.get(id);
gettingBookmarks.then(onFulfilledGetBookmark, error => (console.log("error get bookmark: " + error)));

return gettingBookmarks.then( (results) => {return results[0];});
}

function logInfo(bookmarkInfo, id, action) {
const temp = writeInfo(bookmarkInfo,id, action);
list.push(temp);
}


function onFulfilledGetBookmark(bookmark){
console.log(bookmark);
return bookmark;
}


function writeInfo(bookmarkInfo, id, action)
{
let dateISO = new Date();
console.log(dateISO.toISOString());

console.log(bookmarkInfo.title);
console.log(bookmarkInfo.url);

return {
date: dateISO.toISOString(),
action: action,
type: bookmarkInfo.type,
url: bookmarkInfo.url,
title: bookmarkInfo.title,
id: id,
dateAdded: bookmarkInfo.dateAdded,
dateGroupModified: bookmarkInfo.dateGroupModified
};
}


function getMessage() {
if (!browser.runtime.onMessage.hasListener(processMessage)) {
browser.runtime.onMessage.addListener(processMessage);
}
}

function stopMessage() {
if (browser.runtime.onMessage.hasListener(processMessage)) {
browser.runtime.onMessage.removeListener(processMessage);
}
}

function processMessage(data, sender) {
console.log("MSG Copy " + data.content);

if (data.content === '#start') {
startBrowsingCapture();
return Promise.resolve({response: "done"});
}

if (data.content === '#stop') {
stopBrowsingCapture();

return Promise.resolve({response: "done"});
}

if (data.content === '#status') {
return Promise.resolve({response: status});
}

if (data.content === '#download') {
downloadBrowsingBookmark();
return Promise.resolve({response: "done"});
}

return Promise.resolve({response: "error"});
}

function dateFile() {
let dateUTC = new Date();
let dateString = dateUTC.getUTCFullYear() + "-" + (dateUTC.getUTCMonth() + 1) + "-" + dateUTC.getUTCDate() + "_" +
dateUTC.getUTCHours() + "-" + dateUTC.getUTCMinutes() + "-" + dateUTC.getUTCSeconds();
return dateString;
}


function saveData(data) {
let blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
let url = URL.createObjectURL(blob);
let dateString = "regis-browser-bookmark/" + dateFile() + ".txt";
let download = browser.downloads.download({url: url, filename: dateString, saveAs: false});
download.then(() => {
list = []
}).catch(error => console.log(error));
}


function init() {
startBrowsingCapture();
getMessage();

browser.windows.onRemoved.addListener(() => {
downloadBrowsingBookmark()
});

setInterval(downloadBrowsingBookmark,1200000); // every 20 minutes

}


init();
25 changes: 25 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"manifest_version": 2,
"name": "REGIS-BROWSER-BOOKMARK",
"version": "0.1",
"description": "Capture web browsing bookmark history",
"author": "acendrou",
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_title": "REGIS-BROWSER-BOOKMARK",
"default_popup": "popup/choose_options.html"
},
"permissions": [
"bookmarks",
"downloads"
],
"browser_specific_settings": {
"gecko": {
"id": "regis-browser-bookmark@ekvidi"
}
}
}
34 changes: 34 additions & 0 deletions src/popup/choose_options.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
html, body {
width: 300px;
}

.title {
font-size: medium;
text-align: center;
color: blueviolet;
}

.button {
font-family: Arial, sans-serif;
margin: 3% auto;
padding: 4px;
text-align: center;
font-size: 1.5em;
cursor: pointer;
}

#start {
color: green;
}

#stop {
color: red;
}

#status {
padding-left: 40%
}

#download {
color: black;
}
26 changes: 26 additions & 0 deletions src/popup/choose_options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>REGIS-BROWSER-BOOKMARK</title>
<link rel="stylesheet" href="choose_options.css"/>
</head>
<body>
<div id="title" class="title">REGIS-BROWSER-BOOKMARK</div>
<div id="popup_content">
<div id="start" class="button">
Start
</div>
<div id="stop" class="button">
Stop
</div>
<div id="status">
STARTED
</div>
<div id="download" class="button">
Download
</div>
</div>
<script src="choose_options.js"></script>
</body>
</html>
Loading

0 comments on commit 5a783b0

Please sign in to comment.