Skip to content

Commit

Permalink
try to get window focus and open to mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
felipe-ds-lima committed Feb 8, 2024
1 parent a104993 commit 35104f7
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 30 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ Easily copy image from Obsidian to clipboard by right clicking image.
5. Click Install.
6. Once installed, toggle the plugin on in the list of installed plugins.

## Usage
## Usage In Desktop

1. Right-click on an image to 'Copy Image' to clipboard.

## Usage In Mobile

1. Just touch the image.

Enjoy your enhanced Obsidian experience with the Obsidian Copy Image Plugin!
98 changes: 75 additions & 23 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,83 @@
import { Notice, Plugin } from "obsidian";
import { Notice, Plugin, Platform } from "obsidian";

export default class CopyImagePlugin extends Plugin {
async onload() {
this.registerDomEvent(document, "contextmenu", (evt: MouseEvent) => {
const images = document.getElementsByTagName("img");
(async () => {
for (let i = 0; i < images.length; i++) {
if (images[i].contains(evt.target as Node)) {
const response = await fetch(images[i].src);
const imageBlob = await response.blob();
navigator.clipboard
.write([
new ClipboardItem({
[imageBlob.type]: imageBlob,
}),
])
.then(() => {
new Notice("Copied image URL to clipboard!");
})
.catch(() => {
new Notice("Failed to copy image URL to clipboard!");
});
break;
if (!Platform.isMobile) {
this.registerDomEvent(
document,
"contextmenu",
(evt: MouseEvent) => {
if (Platform.isMacOS) {
const obsidianWindow = window.open(
"obsidian://open",
"_self"
);
if (obsidianWindow) {
obsidianWindow.focus();
} else {
new Notice("Failed to focus Obsidian window.");
}
}

const images = document.getElementsByTagName("img");
(async () => {
for (let i = 0; i < images.length; i++) {
if (images[i].contains(evt.target as Node)) {
const response = await fetch(images[i].src);
const imageBlob = await response.blob();
navigator.clipboard
.write([
new ClipboardItem({
[imageBlob.type]: imageBlob,
}),
])
.then(() => {
new Notice(
"Copied image URL to clipboard!"
);
})
.catch(() => {
new Notice(
"Failed to copy image URL to clipboard!"
);
});
break;
}
}
})();
}
})();
});
);
} else {
// Mobile
this.registerDomEvent(document, "click", (evt: MouseEvent) => {
const images = document.getElementsByTagName("img");
(async () => {
for (let i = 0; i < images.length; i++) {
if (images[i].contains(evt.target as Node)) {
const response = await fetch(images[i].src);
const imageBlob = await response.blob();
navigator.clipboard
.write([
new ClipboardItem({
[imageBlob.type]: imageBlob,
}),
])
.then(() => {
new Notice(
"Copied image URL to clipboard!"
);
})
.catch(() => {
new Notice(
"Failed to copy image URL to clipboard!"
);
});
break;
}
}
})();
});
}
}

onunload() {}
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"id": "copy-image",
"name": "Copy Image",
"version": "1.0.4",
"version": "1.0.5",
"minAppVersion": "0.15.0",
"description": "Easily copy image from Obsidian to clipboard by right clicking image.",
"author": "Felipe D.S. Lima",
"authorUrl": "https://github.com/felipe-ds-lima",
"fundingUrl": "https://www.buymeacoffee.com/felipe.ds.lima",
"isDesktopOnly": true
"isDesktopOnly": false
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "copy-image",
"displayName": "Copy Image",
"version": "1.0.4",
"version": "1.0.5",
"description": "Easily copy image from Obsidian to clipboard by right clicking image.",
"main": "main.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"1.0.3": "0.15.0",
"1.0.4": "0.15.0"
"1.0.4": "0.15.0",
"1.0.5": "0.15.0"
}

0 comments on commit 35104f7

Please sign in to comment.