Skip to content

Commit

Permalink
Merge pull request #138 from yorkxin/firefox-mv2
Browse files Browse the repository at this point in the history
Firefox: Switch back to Manifest V2 (MV2)
  • Loading branch information
yorkxin authored Apr 24, 2024
2 parents 6386d0b + 7e4e8f5 commit f00ca7f
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- run: npm install -g web-ext # build tool
- run: npm run build-chrome
- run: npm run build-firefox
- run: npm run build-firefox-mv2
- run: npm run build-firefox-mv3
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.DS_Store
node_modules/
build/*
firefox-mv3/dist/*
!firefox-mv3/dist/.keep
firefox/dist/*
!firefox/dist/.keep
firefox-mv2/dist/*
!firefox-mv2/dist/.keep
chrome/dist/*
!chrome/dist/.keep
.nyc_output
6 changes: 3 additions & 3 deletions .idea/copy-as-markdown.iml

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

File renamed without changes.
File renamed without changes.
13 changes: 8 additions & 5 deletions firefox-mv2/manifest.json → firefox-mv3/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Copy as Markdown",
"version": "2.13.0",
"manifest_version": 2,
"manifest_version": 3,
"description": "Copy Link or Image as Markdown code",
"permissions": [
"activeTab",
Expand All @@ -12,7 +12,7 @@
"storage",
"tabs"
],
"browser_action": {
"action": {
"default_icon": {
"128": "./dist/images/icon-128.png"
},
Expand All @@ -26,8 +26,11 @@
"128": "./dist/images/icon-128.png"
},
"background": {
"page": "background.html",
"persistent": false
"scripts": [
"hacks.js",
"./dist/background.js"
],
"type": "module"
},
"commands": {
"selection-as-markdown": {
Expand Down Expand Up @@ -67,7 +70,7 @@
"browser_specific_settings": {
"gecko": {
"id": "jid1-tfBgelm3d4bLkQ@jetpack",
"strict_min_version": "60.0"
"strict_min_version": "112.0"
}
}
}
File renamed without changes.
File renamed without changes.
13 changes: 5 additions & 8 deletions firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Copy as Markdown",
"version": "2.13.0",
"manifest_version": 3,
"manifest_version": 2,
"description": "Copy Link or Image as Markdown code",
"permissions": [
"activeTab",
Expand All @@ -12,7 +12,7 @@
"storage",
"tabs"
],
"action": {
"browser_action": {
"default_icon": {
"128": "./dist/images/icon-128.png"
},
Expand All @@ -26,11 +26,8 @@
"128": "./dist/images/icon-128.png"
},
"background": {
"scripts": [
"hacks.js",
"./dist/background.js"
],
"type": "module"
"page": "background.html",
"persistent": true
},
"commands": {
"selection-as-markdown": {
Expand Down Expand Up @@ -70,7 +67,7 @@
"browser_specific_settings": {
"gecko": {
"id": "jid1-tfBgelm3d4bLkQ@jetpack",
"strict_min_version": "112.0"
"strict_min_version": "60.0"
}
}
}
3 changes: 2 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"include": [
"src/**/*",
"node_modules/web-ext-types/global/index.d.ts", "chrome/environment.chrome.js", "firefox/environment.firefox.js"
"node_modules/web-ext-types/global/index.d.ts", "chrome/environment.chrome.js",
"firefox-mv3/environment.firefox.js"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"test": "mocha",
"build-chrome": "sh compile.sh chrome && zip -r build/chrome.zip chrome/",
"build-firefox-mv2": "sh compile.sh firefox-mv2 && npx web-ext build -s firefox-mv2/ -a build/firefox-mv2/",
"build-firefox": "sh compile.sh firefox && npx web-ext build -s firefox/ -a build/firefox/",
"build-firefox-mv3": "sh compile.sh firefox-mv3 && npx web-ext build -s firefox-mv3/ -a build/firefox-mv3/",
"debug-chrome": "npx nodemon --exec 'sh compile.sh chrome' & npx web-ext run -s chrome/ -t chromium --args chrome://extensions https://example.com",
"debug-edge": "npx nodemon --exec 'sh compile.sh chrome' & npx web-ext run -s chrome/ -t chromium --chromium-binary '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge' --args chrome://extensions https://example.com",
"debug-firefox-mv2": "npx nodemon --exec 'sh compile.sh firefox-mv2' & npx web-ext run -s firefox-mv2/ --url about:debugging#/runtime/this-firefox https://example.com",
Expand Down
7 changes: 5 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,14 @@ function getTurndownOptions() {
}

async function convertSelectionInTabToMarkdown(tab) {
await chrome.scripting.executeScript({
// XXX: In Firefox MV2, executeScript() does not return results.
// We must use browser.scripting instead of chrome.scripting .
const entrypoint = (typeof browser !== 'undefined') ? browser.scripting : chrome.scripting;
await entrypoint.executeScript({
target: { tabId: tab.id, allFrames: true },
files: ['dist/vendor/turndown.js'],
});
const results = await chrome.scripting.executeScript({
const results = await entrypoint.executeScript({
target: { tabId: tab.id, allFrames: true },
func: selectionToMarkdown,
args: [
Expand Down
6 changes: 5 additions & 1 deletion src/lib/clipboard-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ async function copy(text) {
*/
export default async function writeUsingContentScript(tab, text) {
return new Promise((resolve, reject) => {
chrome.scripting.executeScript({
// XXX: In Firefox MV2, executeScript() does not return results.
// We must use browser.scripting instead of chrome.scripting .
const entrypoint = (typeof browser !== 'undefined') ? browser.scripting : chrome.scripting;

entrypoint.executeScript({
target: {
tabId: tab.id,
},
Expand Down
22 changes: 17 additions & 5 deletions src/lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ const SKLinkTextAlwaysEscapeBrackets = 'linkTextAlwaysEscapeBrackets';
const SKStyleOfUnorderedList = 'styleOfUnorderedList ';

/**
* Singleton Settings object in the chrome.storage.sync storage
* Singleton Settings object in the sync storage
*/
export default {
/**
* @param {boolean} value
* @return {Promise<void>}
* */
async setLinkTextAlwaysEscapeBrackets(value) {
await chrome.storage.sync.set({
await this.syncStorage.set({
[SKLinkTextAlwaysEscapeBrackets]: value,
});
this.publishUpdated();
},

async setStyleOfUnrderedList(value) {
await chrome.storage.sync.set({
await this.syncStorage.set({
[SKStyleOfUnorderedList]: value,
});
this.publishUpdated();
},

async reset() {
await chrome.storage.sync.clear();
await this.syncStorage.clear();
this.publishUpdated();
},

Expand All @@ -34,7 +34,7 @@ export default {
},

async getAll() {
const all = await chrome.storage.sync.get({
const all = await this.syncStorage.get({
[SKLinkTextAlwaysEscapeBrackets]: false,
[SKStyleOfUnorderedList]: 'dash',
});
Expand All @@ -44,4 +44,16 @@ export default {
styleOfUnorderedList: all[SKStyleOfUnorderedList],
};
},

/**
* @returns {chrome.storage.SyncStorageArea|browser.storage.StorageArea}
*/
get syncStorage() {
// XXX: in Firefox MV2 the implementation of chrome.storage.sync
// always return undefined. We must use browser.storage.sync .
if (typeof browser !== 'undefined') {
return browser.storage.sync;
}
return chrome.storage.sync;
},
};

0 comments on commit f00ca7f

Please sign in to comment.