Skip to content

Commit

Permalink
Migrate to MV3 Blink (#149)
Browse files Browse the repository at this point in the history
Fix #128
  • Loading branch information
asamuzaK committed Apr 7, 2023
1 parent bb7e7bb commit 558d1c6
Show file tree
Hide file tree
Showing 14 changed files with 385 additions and 1,692 deletions.
10 changes: 8 additions & 2 deletions modules/blink.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const createManifest = async info => {
});
const manifest = JSON.parse(srcContent);
const replaceItems = {
browser_action: {
action: {
browser_style: true,
default_icon: {
16: 'img/icon-color-16.png',
Expand All @@ -37,18 +37,23 @@ export const createManifest = async info => {
default_popup: 'html/popup.html',
default_title: '__MSG_extensionName__'
},
background: {
service_worker: 'mjs/background.js',
type: 'module'
},
icons: {
16: 'img/icon-black-16.png',
32: 'img/icon-black-32.png',
48: 'img/icon-color-48.png',
96: 'img/icon-color-96.png',
128: 'img/icon-color-128.png'
},
// TODO: refactoring when switching to MV3
permissions: [
'activeTab',
'clipboardWrite',
'contextMenus',
'offscreen',
'scripting',
'storage',
'tabs'
]
Expand Down Expand Up @@ -105,6 +110,7 @@ export const createPolyfilledJsFile = async (file, info) => {
export const createJsFiles = async info => {
const files = [
'background.js',
'offscreen.js',
'options.js',
'popup.js'
];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"scripts": {
"bundle": "npm-run-all -s bundle-*",
"bundle-compat": "node index compat -ci",
"bundle-copy": "copyfiles --up=1 --verbose src/LICENSE src/_locales/**/*.json src/css/*.css src/html/*.html src/img/*.* src/lib/**/* src/mjs/*.js src/js/*.js --exclude=src/mjs/background.js --exclude=src/mjs/options.js --exclude=src/mjs/popup.js bundle",
"bundle-copy": "copyfiles --up=1 --verbose src/LICENSE src/_locales/**/*.json src/css/*.css src/html/*.html src/img/*.* src/lib/**/* src/mjs/*.js src/js/*.js --exclude=src/mjs/background.js --exclude=src/mjs/exec-copy.js --exclude=src/mjs/offscreen.js --exclude=src/mjs/options.js --exclude=src/mjs/popup.js bundle",
"bundle-repl": "copyfiles --up=2 --verbose src/repl/*.js bundle/mjs",
"include": "npm-run-all -s include-*",
"include-browser": "copyfiles --up=3 --verbose node_modules/webext-schema/modules/browser.js src/mjs",
"include-polyfill": "copyfiles -f --verbose node_modules/webextension-polyfill/LICENSE node_modules/webextension-polyfill/dist/browser-polyfill.min.js node_modules/webextension-polyfill/dist/browser-polyfill.min.js.map src/lib/mozilla && node index include --dir=mozilla -i",
Expand Down
11 changes: 0 additions & 11 deletions src/html/background.html

This file was deleted.

10 changes: 10 additions & 0 deletions src/html/offscreen.html
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>
104 changes: 36 additions & 68 deletions src/mjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
/* shared */
import { sanitizeURL } from '../lib/url/url-sanitizer-wo-dompurify.min.js';
import {
execScriptToTab, execScriptsToTabInOrder, executeScriptToTab, getActiveTab,
getActiveTabId, getAllStorage, getAllTabsInWindow, getHighlightedTab,
getStorage, isScriptingAvailable, isTab, queryTabs, removeStorage, sendMessage
executeScriptToTab, getActiveTab, getActiveTabId, getAllStorage,
getAllTabsInWindow, getHighlightedTab, getStorage, isTab, queryTabs,
removeStorage, sendMessage
} from './browser.js';
import { getType, isObjectNotEmpty, isString, logErr } from './common.js';
import { editContent } from './edit-content.js';
Expand All @@ -28,9 +28,8 @@ import {
COPY_TABS_SELECTED, EXEC_COPY, HTML_HYPER, HTML_PLAIN,
ICON_AUTO, ICON_BLACK, ICON_COLOR, ICON_DARK, ICON_LIGHT, ICON_WHITE,
INCLUDE_TITLE_HTML_HYPER, INCLUDE_TITLE_HTML_PLAIN, INCLUDE_TITLE_MARKDOWN,
JS_CONTEXT_INFO, JS_EDIT_CONTENT, MARKDOWN, MIME_HTML, MIME_PLAIN,
NOTIFY_COPY, OPTIONS_OPEN, PREFER_CANONICAL, PROMPT, TEXT_SEP_LINES,
TEXT_TEXT_URL, USER_INPUT, WEBEXT_ID
JS_CONTEXT_INFO, MARKDOWN, MIME_HTML, MIME_PLAIN, NOTIFY_COPY, OPTIONS_OPEN,
PREFER_CANONICAL, PROMPT, TEXT_SEP_LINES, TEXT_TEXT_URL, USER_INPUT, WEBEXT_ID
} from './constant.js';

/* api */
Expand Down Expand Up @@ -241,35 +240,24 @@ export const getSelectedTabsInfo = async menuItemId => {
* @returns {Promise.<object>} - context info
*/
export const getContextInfo = async tabId => {
// TODO: refactoring when switching to MV3
const useScripting = await isScriptingAvailable();
let info;
if (useScripting) {
if (!Number.isInteger(tabId)) {
tabId = await getActiveTabId();
if (!Number.isInteger(tabId)) {
tabId = await getActiveTabId();
}
const arr = await executeScriptToTab({
files: [JS_CONTEXT_INFO],
target: {
tabId
}
const arr = await executeScriptToTab({
files: [JS_CONTEXT_INFO],
target: {
tabId
}
}).catch(logErr);
if (Array.isArray(arr)) {
const [res] = arr;
if (isObjectNotEmpty(res)) {
if (Object.prototype.hasOwnProperty.call(res, 'error')) {
throw res.error;
}
const { result } = res;
info = result;
}).catch(logErr);
let info;
if (Array.isArray(arr)) {
const [res] = arr;
if (isObjectNotEmpty(res)) {
if (Object.prototype.hasOwnProperty.call(res, 'error')) {
throw res.error;
}
}
} else {
const res = await execScriptToTab({
file: JS_CONTEXT_INFO
});
if (Array.isArray(res)) {
[info] = res;
const { result } = res;
info = result;
}
}
return info ?? null;
Expand Down Expand Up @@ -445,43 +433,23 @@ export const extractClickedData = async (info, tab) => {
}
if (isString(content) && isString(url)) {
if (userOpts.get(PROMPT) && formatId !== BBCODE_URL && !isEdited) {
// TODO: refactoring when switching to MV3
const useScripting = await isScriptingAvailable();
const promptMsg = i18n.getMessage(USER_INPUT, formatTitle);
let editedContent;
if (useScripting) {
const arr = await executeScriptToTab({
args: [content, promptMsg],
func: editContent,
target: {
tabId
}
}).catch(logErr);
if (Array.isArray(arr)) {
const [res] = arr;
if (isObjectNotEmpty(res)) {
if (Object.prototype.hasOwnProperty.call(res, 'error')) {
throw res.error;
}
const { result } = res;
editedContent = result;
}
const arr = await executeScriptToTab({
args: [content, promptMsg],
func: editContent,
target: {
tabId
}
} else {
const editData = {
content,
promptMsg
};
const res = await execScriptsToTabInOrder([
{
code: `window.editContentData = ${JSON.stringify(editData)};`
},
{
file: JS_EDIT_CONTENT
}).catch(logErr);
let editedContent;
if (Array.isArray(arr)) {
const [res] = arr;
if (isObjectNotEmpty(res)) {
if (Object.prototype.hasOwnProperty.call(res, 'error')) {
throw res.error;
}
]);
if (Array.isArray(res)) {
[editedContent] = res;
const { result } = res;
editedContent = result;
}
}
text = createLinkText({
Expand Down Expand Up @@ -603,7 +571,7 @@ export const handleMsg = async msg => {
}
case NOTIFY_COPY: {
if (userOpts.get(NOTIFY_COPY) && value) {
func.push(notifyOnCopy());
func.push(notifyOnCopy(isString(value) ? value : null));
}
break;
}
Expand Down
62 changes: 62 additions & 0 deletions src/mjs/offscreen-main.js
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);
};
15 changes: 15 additions & 0 deletions src/mjs/offscreen.js
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)
);
26 changes: 26 additions & 0 deletions src/repl/exec-copy.js
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
});
};
4 changes: 4 additions & 0 deletions src/web-ext-config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module.exports = {
ignoreFiles: [
'./html/offscreen.html',
'./img/*.png',
'./lib/mozilla',
'./mjs/package.json',
'./mjs/offscreen.js',
'./mjs/offscreen-main.js',
'./repl',
'package.json',
'web-ext-config.js'
]
Expand Down
Loading

0 comments on commit 558d1c6

Please sign in to comment.