-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #128
- Loading branch information
Showing
14 changed files
with
385 additions
and
1,692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>url2clipboard - offscreen</title> | ||
<script src="../mjs/offscreen.js" type="module"></script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* offscreen.js | ||
*/ | ||
|
||
/* shared */ | ||
import { Clip } from './clipboard.js'; | ||
import { EXEC_COPY, MIME_HTML, MIME_PLAIN, NOTIFY_COPY } from './constant.js'; | ||
|
||
/* api */ | ||
const { runtime } = browser; | ||
|
||
/** | ||
* execute copy | ||
* | ||
* @param {object} opt - options | ||
* @returns {?Promise} - sendMessage() | ||
*/ | ||
export const execCopy = async (opt = {}) => { | ||
const { formatTitle, mimeType, notify, text } = opt; | ||
let func; | ||
if (mimeType === MIME_HTML || mimeType === MIME_PLAIN) { | ||
await new Clip(text, mimeType).copy(); | ||
if (notify) { | ||
func = runtime.sendMessage({ | ||
[NOTIFY_COPY]: formatTitle | ||
}); | ||
} | ||
} | ||
return func || null; | ||
}; | ||
|
||
/** | ||
* close window | ||
* | ||
* @returns {void} | ||
*/ | ||
export const closeWindow = () => { | ||
window.close(); | ||
}; | ||
|
||
/** | ||
* handle message | ||
* | ||
* @param {object} msg - message | ||
* @returns {Promise.<Array>} - results of each handler | ||
*/ | ||
export const handleMsg = async msg => { | ||
const func = []; | ||
const items = msg && Object.entries(msg); | ||
if (items) { | ||
for (const item of items) { | ||
const [key, value] = item; | ||
switch (key) { | ||
case EXEC_COPY: | ||
func.push(execCopy(value).then(closeWindow)); | ||
break; | ||
default: | ||
} | ||
} | ||
} | ||
return Promise.all(func); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* offscreen.js | ||
*/ | ||
|
||
/* shared */ | ||
import { throwErr } from './common.js'; | ||
import { handleMsg } from './offscreen-main.js'; | ||
|
||
/* api */ | ||
const { runtime } = browser; | ||
|
||
/* listener */ | ||
runtime.onMessage.addListener((msg, sender) => | ||
handleMsg(msg, sender).catch(throwErr) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* exec-copy.js | ||
*/ | ||
|
||
/* shared */ | ||
import { EXEC_COPY } from '../mjs/constant.js'; | ||
|
||
/* api */ | ||
const { offscreen, runtime } = chrome; | ||
|
||
/** | ||
* execute copy | ||
* | ||
* @param {object} opt - options | ||
* @returns {void} | ||
*/ | ||
export const execCopy = async opt => { | ||
await offscreen.createDocument({ | ||
url: 'html/offscreen.html', | ||
reasons: [offscreen.Reason.CLIPBOARD], | ||
justification: 'Write to clipboard.' | ||
}); | ||
await runtime.sendMessage({ | ||
[EXEC_COPY]: opt | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.