Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
equetzal authored and DT3264 committed Jul 3, 2023
0 parents commit a5c98c4
Show file tree
Hide file tree
Showing 12 changed files with 1,789 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

built-files/bookmarks.pem

*.crx

*.xpi
*.pem
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Preparing everything
Execute `prepare.sh` and you'll have all set

To update the bookmarks use `sudo sed -i 's@^BOOKMARKS.*"@BOOKMARKS="'"${BOOKMARKS}"'"@' /usr/bin/`

# Building chromium extension
To build the chromium extension is through the Pack extension option in the Extension options

# Building firefox extension
To build the firefox extension is enough with compressing the bookmark-bridge folder into an xpi file
```
cd bookmark-bridge && 7z a ../bookmark-bridge.xpi * -r
```

# Notes
This extension requires `python-is-python3` to work properly

# Relevant paths
FF_NATIVE_HOST="/usr/lib/mozilla/native-messaging-hosts"
FF_EXT_PATH="/usr/lib/firefox-esr/distribution/extensions"
CHROME_NATIVE_HOST="/etc/opt/chrome/native-messaging-hosts"
CHROME_EXT_PATH="/opt/google/chrome/extensions"
CHROMIUM_NATIVE_HOST="/etc/chromium/native-messaging-hosts"
CHROMIUM_EXT_PATH="/usr/share/chromium/extensions"
35 changes: 35 additions & 0 deletions app/bookmarks-fetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python

import sys
import json
import struct
import subprocess
import time

def get_data():
result = subprocess.run(['/usr/bin/current-bookmarks'], capture_output=True, text=True).stdout.strip()
return result

# Encode a message for transmission,
# given its content.
def encodeMessage(messageContent):
# https://docs.python.org/3/library/json.html#basic-usage
# To get the most compact JSON representation, you should specify
# (',', ':') to eliminate whitespace.
# We want the most compact representation because the browser rejects
# messages that exceed 1 MB.
encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8')
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}

# Send an encoded message to stdout
def sendMessage(encodedMessage):
sys.stdout.buffer.write(encodedMessage['length'])
sys.stdout.buffer.write(encodedMessage['content'])
sys.stdout.buffer.flush()

while True:
# receivedMessage = getMessage()
# if receivedMessage == "ping":
sendMessage(encodeMessage(get_data()))
time.sleep(60)
3 changes: 3 additions & 0 deletions app/current-bookmarks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
BOOKMARKS="{Codeforces^https://cpciitsur.contest.codeforces.com/}|{Omegaup^https://omegaup.com/}|"
echo "$BOOKMARKS"
68 changes: 68 additions & 0 deletions bookmark-bridge/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function isFirefox() {
return navigator.userAgent.indexOf("Firefox") > -1;
}

let current_browser = isFirefox() ? "firefox" : "chrome";

let port = browser.runtime.connectNative("bookmarks_fetcher");

function removeTrailingBackslash(bookmark){
// To avoid issues while comparing URLs, remove the trailing backslash (if exists)
if(bookmark.url[bookmark.url.length - 1] == '/'){
// Removes the last character
bookmark.url = bookmark.url.slice(0,-1);
}
}

function parseBookmarkString(bookmarkString) {
let bookmarks = [];
let bookmarkList = bookmarkString.split('|');
for (let bookmarkStr of bookmarkList) {
if(bookmarkStr.length==0) continue;
let bookmark = bookmarkStr.split('^');
if (bookmark.length === 2) {
bookmarks.push({
title: bookmark[0].replace("{", ""),
url: bookmark[1].replace("}", ""),
});
}
}
return bookmarks;
}
function bookmarkAlreadyExists(directivesBookmark, toolbarBookmarks) {
// On each url remove the trailing dash (URL normalization)
// in case that either the user didn't added a backslash at the end of the url
// or the browser decided that said URL does not require to have a backslash
let directivesBookmarkUrl = directivesBookmark.url.replace(/\/$/, "");
for (let toolbarBookmark of toolbarBookmarks) {
let toolbarBookmarkUrl = toolbarBookmark.url.replace(/\/$/, "");
if (directivesBookmarkUrl == toolbarBookmarkUrl) {
return true;
}
}
return false;
}
async function bookmarksCallback(directivesBookmarksString) {
console.log("Looking for bookmarks");
let toolbarID = current_browser === "firefox" ? "toolbar_____" : "1";
let toolbarBookmarks = await browser.bookmarks.getChildren(toolbarID);
let directivesBookmarks = parseBookmarkString(directivesBookmarksString);
directivesBookmarks.forEach(directivesBookmark => {
if (!bookmarkAlreadyExists(directivesBookmark, toolbarBookmarks)) {
console.log("Adding", directivesBookmark.title);
browser.bookmarks.create({
"parentId": toolbarID,
"title": directivesBookmark.title,
"url": directivesBookmark.url,
});
}
});
}
// bookmarksCallback("{Codeforces^https://cpciitsur.contest.codeforces.com/}|{Omegaup^https://omegaup.com/}|");
/*
Listen for messages from the app.
*/
port.onMessage.addListener((response) => {
// console.log("Received: " + response);
bookmarksCallback(response);
});
Loading

0 comments on commit a5c98c4

Please sign in to comment.