mirror of
https://github.com/zadam/trilium.git
synced 2025-12-05 15:04:24 +01:00
Reset web-clipper to origin/main baseline for MV3 migration
This commit is contained in:
parent
0c1de7e183
commit
a00ea2e91d
@ -9,9 +9,7 @@ Trilium Web Clipper is a web browser extension which allows user to clip text, s
|
||||
For more details, see the [wiki page](https://github.com/zadam/trilium/wiki/Web-clipper).
|
||||
|
||||
## Keyboard shortcuts
|
||||
|
||||
Keyboard shortcuts are available for most functions:
|
||||
|
||||
* Save selected text: `Ctrl+Shift+S` (Mac: `Cmd+Shift+S`)
|
||||
* Save whole page: `Alt+Shift+S` (Mac: `Opt+Shift+S`)
|
||||
* Save screenshot: `Ctrl+Shift+E` (Mac: `Cmd+Shift+E`)
|
||||
@ -23,5 +21,4 @@ To set custom shortcuts, follow the directions for your browser.
|
||||
**Chrome**: `chrome://extensions/shortcuts`
|
||||
|
||||
## Credits
|
||||
|
||||
Some parts of the code are based on the [Joplin Notes browser extension](https://github.com/laurent22/joplin/tree/master/Clipper).
|
||||
|
||||
@ -1,7 +1,3 @@
|
||||
// Import modules
|
||||
import { randomString } from './utils.js';
|
||||
import { triliumServerFacade } from './trilium_server_facade.js';
|
||||
|
||||
// Keyboard shortcuts
|
||||
chrome.commands.onCommand.addListener(async function (command) {
|
||||
if (command == "saveSelection") {
|
||||
@ -12,6 +8,7 @@ chrome.commands.onCommand.addListener(async function (command) {
|
||||
await saveTabs();
|
||||
} else if (command == "saveCroppedScreenshot") {
|
||||
const activeTab = await getActiveTab();
|
||||
|
||||
await saveCroppedScreenshot(activeTab.url);
|
||||
} else {
|
||||
console.log("Unrecognized command", command);
|
||||
@ -40,7 +37,7 @@ function cropImage(newArea, dataUrl) {
|
||||
|
||||
async function takeCroppedScreenshot(cropRect) {
|
||||
const activeTab = await getActiveTab();
|
||||
const zoom = await chrome.tabs.getZoom(activeTab.id) * globalThis.devicePixelRatio || 1;
|
||||
const zoom = await browser.tabs.getZoom(activeTab.id) * window.devicePixelRatio;
|
||||
|
||||
const newArea = Object.assign({}, cropRect);
|
||||
newArea.x *= zoom;
|
||||
@ -48,7 +45,7 @@ async function takeCroppedScreenshot(cropRect) {
|
||||
newArea.width *= zoom;
|
||||
newArea.height *= zoom;
|
||||
|
||||
const dataUrl = await chrome.tabs.captureVisibleTab(null, { format: 'png' });
|
||||
const dataUrl = await browser.tabs.captureVisibleTab(null, { format: 'png' });
|
||||
|
||||
return await cropImage(newArea, dataUrl);
|
||||
}
|
||||
@ -58,56 +55,61 @@ async function takeWholeScreenshot() {
|
||||
// workaround to save the whole page is to scroll & stitch
|
||||
// example in https://github.com/mrcoles/full-page-screen-capture-chrome-extension
|
||||
// see page.js and popup.js
|
||||
return await chrome.tabs.captureVisibleTab(null, { format: 'png' });
|
||||
return await browser.tabs.captureVisibleTab(null, { format: 'png' });
|
||||
}
|
||||
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
browser.runtime.onInstalled.addListener(() => {
|
||||
if (isDevEnv()) {
|
||||
chrome.action.setIcon({
|
||||
browser.browserAction.setIcon({
|
||||
path: 'icons/32-dev.png',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Context menus
|
||||
chrome.contextMenus.create({
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-selection",
|
||||
title: "Save selection to Trilium",
|
||||
contexts: ["selection"]
|
||||
});
|
||||
|
||||
chrome.contextMenus.create({
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-cropped-screenshot",
|
||||
title: "Clip screenshot to Trilium",
|
||||
contexts: ["page"]
|
||||
});
|
||||
|
||||
chrome.contextMenus.create({
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-cropped-screenshot",
|
||||
title: "Crop screen shot to Trilium",
|
||||
contexts: ["page"]
|
||||
});
|
||||
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-whole-screenshot",
|
||||
title: "Save whole screen shot to Trilium",
|
||||
contexts: ["page"]
|
||||
});
|
||||
|
||||
chrome.contextMenus.create({
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-page",
|
||||
title: "Save whole page to Trilium",
|
||||
contexts: ["page"]
|
||||
});
|
||||
|
||||
chrome.contextMenus.create({
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-link",
|
||||
title: "Save link to Trilium",
|
||||
contexts: ["link"]
|
||||
});
|
||||
|
||||
chrome.contextMenus.create({
|
||||
browser.contextMenus.create({
|
||||
id: "trilium-save-image",
|
||||
title: "Save image to Trilium",
|
||||
contexts: ["image"]
|
||||
});
|
||||
|
||||
async function getActiveTab() {
|
||||
const tabs = await chrome.tabs.query({
|
||||
const tabs = await browser.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
});
|
||||
@ -116,7 +118,7 @@ async function getActiveTab() {
|
||||
}
|
||||
|
||||
async function getWindowTabs() {
|
||||
const tabs = await chrome.tabs.query({
|
||||
const tabs = await browser.tabs.query({
|
||||
currentWindow: true
|
||||
});
|
||||
|
||||
@ -130,80 +132,21 @@ async function sendMessageToActiveTab(message) {
|
||||
throw new Error("No active tab.");
|
||||
}
|
||||
|
||||
// In Manifest V3, we need to inject content script if not already present
|
||||
try {
|
||||
return await chrome.tabs.sendMessage(activeTab.id, message);
|
||||
} catch (error) {
|
||||
// Content script might not be injected, try to inject it
|
||||
try {
|
||||
await chrome.scripting.executeScript({
|
||||
target: { tabId: activeTab.id },
|
||||
files: ['content.js']
|
||||
});
|
||||
|
||||
// Wait a bit for the script to initialize
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
|
||||
return await chrome.tabs.sendMessage(activeTab.id, message);
|
||||
} catch (injectionError) {
|
||||
console.error('Failed to inject content script:', injectionError);
|
||||
throw new Error(`Failed to communicate with page: ${injectionError.message}`);
|
||||
return await browser.tabs.sendMessage(activeTab.id, message);
|
||||
}
|
||||
catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async function toast(message, noteId = null, tabIds = null) {
|
||||
try {
|
||||
await sendMessageToActiveTab({
|
||||
function toast(message, noteId = null, tabIds = null) {
|
||||
sendMessageToActiveTab({
|
||||
name: 'toast',
|
||||
message: message,
|
||||
noteId: noteId,
|
||||
tabIds: tabIds
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to show toast:', error);
|
||||
}
|
||||
}
|
||||
|
||||
function showStatusToast(message, isProgress = true) {
|
||||
// Make this completely async and fire-and-forget
|
||||
// Only try to send status if we're confident the content script will be ready
|
||||
(async () => {
|
||||
try {
|
||||
// Test if content script is ready with a quick ping
|
||||
const activeTab = await getActiveTab();
|
||||
if (!activeTab) return;
|
||||
|
||||
await chrome.tabs.sendMessage(activeTab.id, { name: 'ping' });
|
||||
// If ping succeeds, send the status toast
|
||||
await chrome.tabs.sendMessage(activeTab.id, {
|
||||
name: 'status-toast',
|
||||
message: message,
|
||||
isProgress: isProgress
|
||||
});
|
||||
} catch (error) {
|
||||
// Content script not ready or failed - silently skip
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
function updateStatusToast(message, isProgress = true) {
|
||||
// Make this completely async and fire-and-forget
|
||||
(async () => {
|
||||
try {
|
||||
const activeTab = await getActiveTab();
|
||||
if (!activeTab) return;
|
||||
|
||||
// Direct message without injection logic since content script should be ready by now
|
||||
await chrome.tabs.sendMessage(activeTab.id, {
|
||||
name: 'update-status-toast',
|
||||
message: message,
|
||||
isProgress: isProgress
|
||||
});
|
||||
} catch (error) {
|
||||
// Content script not ready or failed - silently skip
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
function blob2base64(blob) {
|
||||
@ -239,7 +182,7 @@ async function postProcessImage(image) {
|
||||
}
|
||||
|
||||
async function postProcessImages(resp) {
|
||||
if (resp && resp.images) {
|
||||
if (resp.images) {
|
||||
for (const image of resp.images) {
|
||||
await postProcessImage(image);
|
||||
}
|
||||
@ -247,32 +190,17 @@ async function postProcessImages(resp) {
|
||||
}
|
||||
|
||||
async function saveSelection() {
|
||||
showStatusToast("📝 Capturing selection...");
|
||||
|
||||
const payload = await sendMessageToActiveTab({name: 'trilium-save-selection'});
|
||||
|
||||
if (!payload) {
|
||||
console.error('No payload received from content script');
|
||||
updateStatusToast("❌ Failed to capture selection", false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.images && payload.images.length > 0) {
|
||||
updateStatusToast(`🖼️ Processing ${payload.images.length} image(s)...`);
|
||||
}
|
||||
await postProcessImages(payload);
|
||||
|
||||
const triliumType = triliumServerFacade.triliumSearch?.status === 'found-desktop' ? 'Desktop' : 'Server';
|
||||
updateStatusToast(`💾 Saving to Trilium ${triliumType}...`);
|
||||
|
||||
const resp = await triliumServerFacade.callService('POST', 'clippings', payload);
|
||||
|
||||
if (!resp) {
|
||||
updateStatusToast("❌ Failed to save to Trilium", false);
|
||||
return;
|
||||
}
|
||||
|
||||
await toast("✅ Selection has been saved to Trilium.", resp.noteId);
|
||||
toast("Selection has been saved to Trilium.", resp.noteId);
|
||||
}
|
||||
|
||||
async function getImagePayloadFromSrc(src, pageUrl) {
|
||||
@ -294,46 +222,33 @@ async function getImagePayloadFromSrc(src, pageUrl) {
|
||||
}
|
||||
|
||||
async function saveCroppedScreenshot(pageUrl) {
|
||||
showStatusToast("📷 Preparing screenshot...");
|
||||
|
||||
const cropRect = await sendMessageToActiveTab({name: 'trilium-get-rectangle-for-screenshot'});
|
||||
|
||||
updateStatusToast("📸 Capturing screenshot...");
|
||||
const src = await takeCroppedScreenshot(cropRect);
|
||||
|
||||
const payload = await getImagePayloadFromSrc(src, pageUrl);
|
||||
|
||||
const triliumType = triliumServerFacade.triliumSearch?.status === 'found-desktop' ? 'Desktop' : 'Server';
|
||||
updateStatusToast(`💾 Saving to Trilium ${triliumType}...`);
|
||||
|
||||
const resp = await triliumServerFacade.callService("POST", "clippings", payload);
|
||||
|
||||
if (!resp) {
|
||||
updateStatusToast("❌ Failed to save screenshot", false);
|
||||
return;
|
||||
}
|
||||
|
||||
await toast("✅ Screenshot has been saved to Trilium.", resp.noteId);
|
||||
toast("Screenshot has been saved to Trilium.", resp.noteId);
|
||||
}
|
||||
|
||||
async function saveWholeScreenshot(pageUrl) {
|
||||
showStatusToast("📸 Capturing full screenshot...");
|
||||
|
||||
const src = await takeWholeScreenshot();
|
||||
|
||||
const payload = await getImagePayloadFromSrc(src, pageUrl);
|
||||
|
||||
const triliumType = triliumServerFacade.triliumSearch?.status === 'found-desktop' ? 'Desktop' : 'Server';
|
||||
updateStatusToast(`💾 Saving to Trilium ${triliumType}...`);
|
||||
|
||||
const resp = await triliumServerFacade.callService("POST", "clippings", payload);
|
||||
|
||||
if (!resp) {
|
||||
updateStatusToast("❌ Failed to save screenshot", false);
|
||||
return;
|
||||
}
|
||||
|
||||
await toast("✅ Screenshot has been saved to Trilium.", resp.noteId);
|
||||
toast("Screenshot has been saved to Trilium.", resp.noteId);
|
||||
}
|
||||
|
||||
async function saveImage(srcUrl, pageUrl) {
|
||||
@ -345,40 +260,21 @@ async function saveImage(srcUrl, pageUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
await toast("Image has been saved to Trilium.", resp.noteId);
|
||||
toast("Image has been saved to Trilium.", resp.noteId);
|
||||
}
|
||||
|
||||
async function saveWholePage() {
|
||||
// Step 1: Show initial status (completely non-blocking)
|
||||
showStatusToast("📄 Page capture started...");
|
||||
|
||||
const payload = await sendMessageToActiveTab({name: 'trilium-save-page'});
|
||||
|
||||
if (!payload) {
|
||||
console.error('No payload received from content script');
|
||||
updateStatusToast("❌ Failed to capture page content", false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 2: Processing images
|
||||
if (payload.images && payload.images.length > 0) {
|
||||
updateStatusToast(`🖼️ Processing ${payload.images.length} image(s)...`);
|
||||
}
|
||||
await postProcessImages(payload);
|
||||
|
||||
// Step 3: Saving to Trilium
|
||||
const triliumType = triliumServerFacade.triliumSearch?.status === 'found-desktop' ? 'Desktop' : 'Server';
|
||||
updateStatusToast(`💾 Saving to Trilium ${triliumType}...`);
|
||||
|
||||
const resp = await triliumServerFacade.callService('POST', 'notes', payload);
|
||||
|
||||
if (!resp) {
|
||||
updateStatusToast("❌ Failed to save to Trilium", false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 4: Success with link
|
||||
await toast("✅ Page has been saved to Trilium.", resp.noteId);
|
||||
toast("Page has been saved to Trilium.", resp.noteId);
|
||||
}
|
||||
|
||||
async function saveLinkWithNote(title, content) {
|
||||
@ -399,7 +295,7 @@ async function saveLinkWithNote(title, content) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await toast("Link with note has been saved to Trilium.", resp.noteId);
|
||||
toast("Link with note has been saved to Trilium.", resp.noteId);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -445,16 +341,10 @@ async function saveTabs() {
|
||||
|
||||
const tabIds = tabs.map(tab=>{return tab.id});
|
||||
|
||||
await toast(`${tabs.length} links have been saved to Trilium.`, resp.noteId, tabIds);
|
||||
toast(`${tabs.length} links have been saved to Trilium.`, resp.noteId, tabIds);
|
||||
}
|
||||
|
||||
// Helper function
|
||||
function isDevEnv() {
|
||||
const manifest = chrome.runtime.getManifest();
|
||||
return manifest.name.endsWith('(dev)');
|
||||
}
|
||||
|
||||
chrome.contextMenus.onClicked.addListener(async function(info, tab) {
|
||||
browser.contextMenus.onClicked.addListener(async function(info, tab) {
|
||||
if (info.menuItemId === 'trilium-save-selection') {
|
||||
await saveSelection();
|
||||
}
|
||||
@ -485,7 +375,7 @@ chrome.contextMenus.onClicked.addListener(async function(info, tab) {
|
||||
return;
|
||||
}
|
||||
|
||||
await toast("Link has been saved to Trilium.", resp.noteId);
|
||||
toast("Link has been saved to Trilium.", resp.noteId);
|
||||
}
|
||||
else if (info.menuItemId === 'trilium-save-page') {
|
||||
await saveWholePage();
|
||||
@ -495,7 +385,7 @@ chrome.contextMenus.onClicked.addListener(async function(info, tab) {
|
||||
}
|
||||
});
|
||||
|
||||
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
|
||||
browser.runtime.onMessage.addListener(async request => {
|
||||
console.log("Received", request);
|
||||
|
||||
if (request.name === 'openNoteInTrilium') {
|
||||
@ -507,14 +397,14 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
|
||||
|
||||
// desktop app is not available so we need to open in browser
|
||||
if (resp.result === 'open-in-browser') {
|
||||
const {triliumServerUrl} = await chrome.storage.sync.get("triliumServerUrl");
|
||||
const {triliumServerUrl} = await browser.storage.sync.get("triliumServerUrl");
|
||||
|
||||
if (triliumServerUrl) {
|
||||
const noteUrl = triliumServerUrl + '/#' + request.noteId;
|
||||
|
||||
console.log("Opening new tab in browser", noteUrl);
|
||||
|
||||
chrome.tabs.create({
|
||||
browser.tabs.create({
|
||||
url: noteUrl
|
||||
});
|
||||
}
|
||||
@ -524,20 +414,19 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
|
||||
}
|
||||
}
|
||||
else if (request.name === 'closeTabs') {
|
||||
return await chrome.tabs.remove(request.tabIds)
|
||||
return await browser.tabs.remove(request.tabIds)
|
||||
}
|
||||
else if (request.name === 'load-script') {
|
||||
return await chrome.scripting.executeScript({
|
||||
target: { tabId: sender.tab?.id },
|
||||
files: [request.file]
|
||||
});
|
||||
return await browser.tabs.executeScript({file: request.file});
|
||||
}
|
||||
else if (request.name === 'save-cropped-screenshot') {
|
||||
const activeTab = await getActiveTab();
|
||||
|
||||
return await saveCroppedScreenshot(activeTab.url);
|
||||
}
|
||||
else if (request.name === 'save-whole-screenshot') {
|
||||
const activeTab = await getActiveTab();
|
||||
|
||||
return await saveWholeScreenshot(activeTab.url);
|
||||
}
|
||||
else if (request.name === 'save-whole-page') {
|
||||
@ -559,7 +448,4 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
|
||||
const activeTab = await getActiveTab();
|
||||
triliumServerFacade.triggerSearchNoteByUrl(activeTab.url);
|
||||
}
|
||||
|
||||
// Important: return true to indicate async response
|
||||
return true;
|
||||
});
|
||||
|
||||
@ -1,33 +1,3 @@
|
||||
// Utility functions (inline to avoid module dependency issues)
|
||||
function randomString(len) {
|
||||
let text = "";
|
||||
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
for (let i = 0; i < len; i++) {
|
||||
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
function getBaseUrl() {
|
||||
let output = getPageLocationOrigin() + location.pathname;
|
||||
|
||||
if (output[output.length - 1] !== '/') {
|
||||
output = output.split('/');
|
||||
output.pop();
|
||||
output = output.join('/');
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function getPageLocationOrigin() {
|
||||
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
|
||||
// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.
|
||||
return location.protocol === 'file:' ? 'file://' : location.origin;
|
||||
}
|
||||
|
||||
function absoluteUrl(url) {
|
||||
if (!url) {
|
||||
return url;
|
||||
@ -265,7 +235,7 @@ function createLink(clickAction, text, color = "lightskyblue") {
|
||||
link.style.color = color;
|
||||
link.appendChild(document.createTextNode(text));
|
||||
link.addEventListener("click", () => {
|
||||
chrome.runtime.sendMessage(null, clickAction)
|
||||
browser.runtime.sendMessage(null, clickAction)
|
||||
});
|
||||
|
||||
return link
|
||||
@ -274,10 +244,7 @@ function createLink(clickAction, text, color = "lightskyblue") {
|
||||
async function prepareMessageResponse(message) {
|
||||
console.info('Message: ' + message.name);
|
||||
|
||||
if (message.name === "ping") {
|
||||
return { success: true };
|
||||
}
|
||||
else if (message.name === "toast") {
|
||||
if (message.name === "toast") {
|
||||
let messageText;
|
||||
|
||||
if (message.noteId) {
|
||||
@ -310,42 +277,6 @@ async function prepareMessageResponse(message) {
|
||||
duration: 7000
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true }; // Return a response
|
||||
}
|
||||
else if (message.name === "status-toast") {
|
||||
await requireLib('/lib/toast.js');
|
||||
|
||||
// Hide any existing status toast
|
||||
if (window.triliumStatusToast && window.triliumStatusToast.hide) {
|
||||
window.triliumStatusToast.hide();
|
||||
}
|
||||
|
||||
// Store reference to the status toast so we can replace it
|
||||
window.triliumStatusToast = showToast(message.message, {
|
||||
settings: {
|
||||
duration: message.isProgress ? 60000 : 5000 // Long duration for progress, shorter for errors
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true }; // Return a response
|
||||
}
|
||||
else if (message.name === "update-status-toast") {
|
||||
await requireLib('/lib/toast.js');
|
||||
|
||||
// Hide the previous status toast
|
||||
if (window.triliumStatusToast && window.triliumStatusToast.hide) {
|
||||
window.triliumStatusToast.hide();
|
||||
}
|
||||
|
||||
// Show new toast with updated message
|
||||
window.triliumStatusToast = showToast(message.message, {
|
||||
settings: {
|
||||
duration: message.isProgress ? 60000 : 5000
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true }; // Return a response
|
||||
}
|
||||
else if (message.name === "trilium-save-selection") {
|
||||
const container = document.createElement('div');
|
||||
@ -407,10 +338,7 @@ async function prepareMessageResponse(message) {
|
||||
}
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
prepareMessageResponse(message).then(sendResponse);
|
||||
return true; // Important: indicates async response
|
||||
});
|
||||
browser.runtime.onMessage.addListener(prepareMessageResponse);
|
||||
|
||||
const loadedLibs = [];
|
||||
|
||||
@ -418,6 +346,6 @@ async function requireLib(libPath) {
|
||||
if (!loadedLibs.includes(libPath)) {
|
||||
loadedLibs.push(libPath);
|
||||
|
||||
await chrome.runtime.sendMessage({name: 'load-script', file: libPath});
|
||||
await browser.runtime.sendMessage({name: 'load-script', file: libPath});
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
{
|
||||
"manifest_version": 3,
|
||||
"manifest_version": 2,
|
||||
"name": "Trilium Web Clipper (dev)",
|
||||
"version": "1.0.1",
|
||||
"description": "Save web clippings to Trilium Notes.",
|
||||
"homepage_url": "https://github.com/zadam/trilium-web-clipper",
|
||||
"content_security_policy": {
|
||||
"extension_pages": "script-src 'self'; object-src 'self'"
|
||||
},
|
||||
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
|
||||
"icons": {
|
||||
"32": "icons/32.png",
|
||||
"48": "icons/48.png",
|
||||
@ -15,30 +13,37 @@
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"tabs",
|
||||
"storage",
|
||||
"contextMenus",
|
||||
"scripting"
|
||||
],
|
||||
"host_permissions": [
|
||||
"http://*/",
|
||||
"https://*/"
|
||||
"https://*/",
|
||||
"<all_urls>",
|
||||
"storage",
|
||||
"contextMenus"
|
||||
],
|
||||
"action": {
|
||||
"browser_action": {
|
||||
"default_icon": "icons/32.png",
|
||||
"default_title": "Trilium Web Clipper",
|
||||
"default_popup": "popup/popup.html"
|
||||
},
|
||||
"content_scripts": [],
|
||||
"background": {
|
||||
"service_worker": "background.js",
|
||||
"type": "module"
|
||||
},
|
||||
"web_accessible_resources": [
|
||||
"content_scripts": [
|
||||
{
|
||||
"resources": ["lib/*", "utils.js", "trilium_server_facade.js", "content.js"],
|
||||
"matches": ["<all_urls>"]
|
||||
"matches": [
|
||||
"<all_urls>"
|
||||
],
|
||||
"js": [
|
||||
"lib/browser-polyfill.js",
|
||||
"utils.js",
|
||||
"content.js"
|
||||
]
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"scripts": [
|
||||
"lib/browser-polyfill.js",
|
||||
"utils.js",
|
||||
"trilium_server_facade.js",
|
||||
"background.js"
|
||||
]
|
||||
},
|
||||
"options_ui": {
|
||||
"page": "options/options.html"
|
||||
},
|
||||
|
||||
@ -56,7 +56,7 @@ async function saveTriliumServerSetup(e) {
|
||||
|
||||
$triliumServerPassword.val('');
|
||||
|
||||
chrome.storage.sync.set({
|
||||
browser.storage.sync.set({
|
||||
triliumServerUrl: $triliumServerUrl.val(),
|
||||
authToken: json.token
|
||||
});
|
||||
@ -73,7 +73,7 @@ const $resetTriliumServerSetupLink = $("#reset-trilium-server-setup");
|
||||
$resetTriliumServerSetupLink.on("click", e => {
|
||||
e.preventDefault();
|
||||
|
||||
chrome.storage.sync.set({
|
||||
browser.storage.sync.set({
|
||||
triliumServerUrl: '',
|
||||
authToken: ''
|
||||
});
|
||||
@ -97,7 +97,7 @@ $triilumDesktopSetupForm.on("submit", e => {
|
||||
return;
|
||||
}
|
||||
|
||||
chrome.storage.sync.set({
|
||||
browser.storage.sync.set({
|
||||
triliumDesktopPort: port
|
||||
});
|
||||
|
||||
@ -105,8 +105,8 @@ $triilumDesktopSetupForm.on("submit", e => {
|
||||
});
|
||||
|
||||
async function restoreOptions() {
|
||||
const {triliumServerUrl} = await chrome.storage.sync.get("triliumServerUrl");
|
||||
const {authToken} = await chrome.storage.sync.get("authToken");
|
||||
const {triliumServerUrl} = await browser.storage.sync.get("triliumServerUrl");
|
||||
const {authToken} = await browser.storage.sync.get("authToken");
|
||||
|
||||
$errorMessage.hide();
|
||||
$successMessage.hide();
|
||||
@ -127,7 +127,7 @@ async function restoreOptions() {
|
||||
$triliumServerConfiguredDiv.hide();
|
||||
}
|
||||
|
||||
const {triliumDesktopPort} = await chrome.storage.sync.get("triliumDesktopPort");
|
||||
const {triliumDesktopPort} = await browser.storage.sync.get("triliumDesktopPort");
|
||||
|
||||
$triliumDesktopPort.val(triliumDesktopPort);
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
async function sendMessage(message) {
|
||||
try {
|
||||
return await chrome.runtime.sendMessage(message);
|
||||
return await browser.runtime.sendMessage(message);
|
||||
}
|
||||
catch (e) {
|
||||
console.log("Calling browser runtime failed:", e);
|
||||
@ -15,7 +15,7 @@ const $saveWholeScreenShotButton = $("#save-whole-screenshot-button");
|
||||
const $saveWholePageButton = $("#save-whole-page-button");
|
||||
const $saveTabsButton = $("#save-tabs-button");
|
||||
|
||||
$showOptionsButton.on("click", () => chrome.runtime.openOptionsPage());
|
||||
$showOptionsButton.on("click", () => browser.runtime.openOptionsPage());
|
||||
|
||||
$saveCroppedScreenShotButton.on("click", () => {
|
||||
sendMessage({name: 'save-cropped-screenshot'});
|
||||
@ -115,7 +115,7 @@ const $connectionStatus = $("#connection-status");
|
||||
const $needsConnection = $(".needs-connection");
|
||||
const $alreadyVisited = $("#already-visited");
|
||||
|
||||
chrome.runtime.onMessage.addListener(request => {
|
||||
browser.runtime.onMessage.addListener(request => {
|
||||
if (request.name === 'trilium-search-status') {
|
||||
const {triliumSearch} = request;
|
||||
|
||||
@ -146,7 +146,7 @@ chrome.runtime.onMessage.addListener(request => {
|
||||
if (isConnected) {
|
||||
$needsConnection.removeAttr("disabled");
|
||||
$needsConnection.removeAttr("title");
|
||||
chrome.runtime.sendMessage({name: "trigger-trilium-search-note-url"});
|
||||
browser.runtime.sendMessage({name: "trigger-trilium-search-note-url"});
|
||||
}
|
||||
else {
|
||||
$needsConnection.attr("disabled", "disabled");
|
||||
@ -172,9 +172,9 @@ chrome.runtime.onMessage.addListener(request => {
|
||||
const $checkConnectionButton = $("#check-connection-button");
|
||||
|
||||
$checkConnectionButton.on("click", () => {
|
||||
chrome.runtime.sendMessage({
|
||||
browser.runtime.sendMessage({
|
||||
name: "trigger-trilium-search"
|
||||
})
|
||||
});
|
||||
|
||||
$(() => chrome.runtime.sendMessage({name: "send-trilium-search-status"}));
|
||||
$(() => browser.runtime.sendMessage({name: "send-trilium-search-status"}));
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const PROTOCOL_VERSION_MAJOR = 1;
|
||||
|
||||
function isDevEnv() {
|
||||
const manifest = chrome.runtime.getManifest();
|
||||
const manifest = browser.runtime.getManifest();
|
||||
|
||||
return manifest.name.endsWith('(dev)');
|
||||
}
|
||||
@ -16,7 +16,7 @@ class TriliumServerFacade {
|
||||
|
||||
async sendTriliumSearchStatusToPopup() {
|
||||
try {
|
||||
await chrome.runtime.sendMessage({
|
||||
await browser.runtime.sendMessage({
|
||||
name: "trilium-search-status",
|
||||
triliumSearch: this.triliumSearch
|
||||
});
|
||||
@ -25,7 +25,7 @@ class TriliumServerFacade {
|
||||
}
|
||||
async sendTriliumSearchNoteToPopup(){
|
||||
try{
|
||||
await chrome.runtime.sendMessage({
|
||||
await browser.runtime.sendMessage({
|
||||
name: "trilium-previously-visited",
|
||||
searchNote: this.triliumSearchNote
|
||||
})
|
||||
@ -95,8 +95,8 @@ class TriliumServerFacade {
|
||||
// continue
|
||||
}
|
||||
|
||||
const {triliumServerUrl} = await chrome.storage.sync.get("triliumServerUrl");
|
||||
const {authToken} = await chrome.storage.sync.get("authToken");
|
||||
const {triliumServerUrl} = await browser.storage.sync.get("triliumServerUrl");
|
||||
const {authToken} = await browser.storage.sync.get("authToken");
|
||||
|
||||
if (triliumServerUrl && authToken) {
|
||||
try {
|
||||
@ -162,7 +162,7 @@ class TriliumServerFacade {
|
||||
}
|
||||
|
||||
async getPort() {
|
||||
const {triliumDesktopPort} = await chrome.storage.sync.get("triliumDesktopPort");
|
||||
const {triliumDesktopPort} = await browser.storage.sync.get("triliumDesktopPort");
|
||||
|
||||
if (triliumDesktopPort) {
|
||||
return parseInt(triliumDesktopPort);
|
||||
@ -222,5 +222,4 @@ class TriliumServerFacade {
|
||||
}
|
||||
}
|
||||
|
||||
export const triliumServerFacade = new TriliumServerFacade();
|
||||
export { TriliumServerFacade };
|
||||
window.triliumServerFacade = new TriliumServerFacade();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export function randomString(len) {
|
||||
function randomString(len) {
|
||||
let text = "";
|
||||
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
@ -9,7 +9,7 @@ export function randomString(len) {
|
||||
return text;
|
||||
}
|
||||
|
||||
export function getBaseUrl() {
|
||||
function getBaseUrl() {
|
||||
let output = getPageLocationOrigin() + location.pathname;
|
||||
|
||||
if (output[output.length - 1] !== '/') {
|
||||
@ -21,7 +21,7 @@ export function getBaseUrl() {
|
||||
return output;
|
||||
}
|
||||
|
||||
export function getPageLocationOrigin() {
|
||||
function getPageLocationOrigin() {
|
||||
// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)
|
||||
// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.
|
||||
return location.protocol === 'file:' ? 'file://' : location.origin;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user