Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ A chrome extension that lets you add the current tab or a note to your [Shaarli]

Available on the [Chrome Web Store](https://chrome.google.com/webstore/detail/add-to-shaarli/jhfblapoehcfajokolimghdfmeeakbee).

## Features

- **Click the extension icon** to add the current tab.
- **Right-click context menu** to easily add the current page or a link.
- **Keyboard shortcuts** for quick access.

## Keyboard shortcuts

| Key combination | Action |
Expand Down
15 changes: 15 additions & 0 deletions addNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
!function(){
chrome.storage.sync.get({
savedShaarliInstance: "https://example.com/"
}, function (items) {
const shaarliInstance = items.savedShaarliInstance;
const popup = window.open(
shaarliInstance +
"?post=" +
"&description=" +
encodeURIComponent(document.getSelection()),
"_blank",
"menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1"
);
});
}();
21 changes: 21 additions & 0 deletions addUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
!function(){
chrome.storage.sync.get({
savedShaarliInstance: "https://example.com/"
}, function (items) {
const shaarliInstance = items.savedShaarliInstance;
const o = location.href;
const e = document.title || o;
const popup = window.open(
shaarliInstance +
"?post=" +
encodeURIComponent(o) +
"&title=" +
encodeURIComponent(e) +
"&description=" +
encodeURIComponent(document.getSelection()) +
"&source=bookmarklet",
"_blank",
"menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1"
);
});
}();
111 changes: 75 additions & 36 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,91 @@
const defaultShaarliInstance = "https://example.com/";

function addUrl() {
chrome.storage.sync.get({
savedShaarliInstance: defaultShaarliInstance
}, function (items) {
let shaarliInstance = items.savedShaarliInstance;
let myCode = "!function(){var o=location.href,e=document.title||o;const popup=window.open('" +
shaarliInstance +
"?post='+encodeURIComponent(o)+'&title='+encodeURIComponent(e)+'&description='+encodeURIComponent(document.getSelection())+'&source=bookmarklet','_blank','menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1')}();";
executeScript(shaarliInstance, myCode);
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === "install") {
chrome.runtime.openOptionsPage();
}
chrome.contextMenus.create({
id: "add-to-shaarli",
title: "Add to Shaarli",
contexts: ["page", "selection", "link"]
});
}
});

function addNote() {
chrome.storage.sync.get({
savedShaarliInstance: defaultShaarliInstance
}, function (items) {
let shaarliInstance = items.savedShaarliInstance;
let myCode = "!function(){var e=document.title;const popup=window.open('" +
shaarliInstance +
"?post='+'&description='+encodeURIComponent(document.getSelection()),'_blank','menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1')}();";
executeScript(shaarliInstance, myCode);
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "add-to-shaarli") {
addUrl(tab);
}
});

function addUrl(tab) {
chrome.storage.sync.get(
{
savedShaarliInstance: defaultShaarliInstance,
},
function (items) {
const shaarliInstance = items.savedShaarliInstance;
if (
shaarliInstance === defaultShaarliInstance ||
shaarliInstance === ""
) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
alert(
"Please setup your Shaarli instance in the options of the extension."
);
},
});
} else {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["addUrl.js"],
});
}
}
);
}

function executeScript(shaarliInstance, code) {
if (shaarliInstance === defaultShaarliInstance || shaarliInstance === "")
code = "alert('Please setup your Shaarli instance in the options of the extension.')";
chrome.tabs.query({
active: true
}, function () {
chrome.tabs.executeScript({
code: code
});
});
function addNote(tab) {
chrome.storage.sync.get(
{
savedShaarliInstance: defaultShaarliInstance,
},
function (items) {
const shaarliInstance = items.savedShaarliInstance;
if (
shaarliInstance === defaultShaarliInstance ||
shaarliInstance === ""
) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
alert(
"Please setup your Shaarli instance in the options of the extension."
);
},
});
} else {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["addNote.js"],
});
}
}
);
}

chrome.commands.onCommand.addListener(function (command) {
chrome.commands.onCommand.addListener(function (command, tab) {
switch (command) {
case "add-url":
addUrl();
addUrl(tab);
break;
case "add-note":
addNote();
addNote(tab);
break;
}
});

chrome.browserAction.onClicked.addListener(function () {
addUrl();
});
chrome.action.onClicked.addListener(function (tab) {
addUrl(tab);
});
26 changes: 13 additions & 13 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "Add to Shaarli",
"description": "This extension lets you add the current tab or a note to your Shaarli instance.",
"version": "1.1.0",
Expand All @@ -8,24 +8,21 @@
"48": "icon48.png",
"128": "icon128.png"
},
"browser_action": {
"action": {
"default_icon": "icon.png"
},

"options_page": "options.html",
"options_ui": {
"page": "options.html",
"open_in_tab": false
},
"background": {
"scripts": [
"background.js"
],
"persistent": false
"service_worker": "background.js"
},
"permissions": [
"storage",
"activeTab",
"storage"
"scripting",
"contextMenus"
],
"host_permissions": [
"<all_urls>"
],
"commands": {
"add-url": {
Expand All @@ -40,5 +37,8 @@
},
"description": "Add a note"
}
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
}
}
}