Skip to content

Commit

Permalink
finished porting to manifest v3
Browse files Browse the repository at this point in the history
  • Loading branch information
GChristensen committed Jun 8, 2022
1 parent af20731 commit 8d4a5e5
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ the [online help](https://gchristensen.github.io/scrapyard/addon/ui/locales/en/h
* [DONE] Add ability to browser synchronized bookmarks to the android application
* [DONE] Add ability to capture entire sites a la WebScrapBook
* [DONE] Add ability to undo bookmark deletions

### Manifest v3 Status

The addon is succesfully ported to the manifest v3 as it is
[implemented](https://extensionworkshop.com/documentation/develop/manifest-v3-migration-guide/)
in Firefox Nightly v102. To run with MV3 rename manifest.json.mv3 to manifest.json.
7 changes: 6 additions & 1 deletion addon/backend_native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UUID from "./lib/uuid.js"
import {settings} from "./settings.js"
import {showNotification} from "./utils_browser.js";
import {hasCSRPermission, showNotification} from "./utils_browser.js";

class NativeBackend {
constructor() {
Expand Down Expand Up @@ -55,9 +55,14 @@ class NativeBackend {
}

async probe(verbose = false) {
if (!await hasCSRPermission())
return false;

const port = await this.getPort();

if (!port && verbose)
showNotification({message: "Can not connect to the helper application."})

return !!port;
}

Expand Down
9 changes: 6 additions & 3 deletions addon/bookmarking.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {settings} from "./settings.js";
import {
getActiveTab, injectCSSFile, injectScriptFile,
getActiveTab, hasCSRPermission, injectCSSFile, injectScriptFile,
openContainerTab,
openPage,
scriptsAllowed,
Expand Down Expand Up @@ -141,6 +141,9 @@ function startSavePageCapture(tab, bookmark, selection) {
}

async function injectSavePageScripts(tab, onError) {
if (!await hasCSRPermission())
return;

try {
try {
await injectScriptFile(tab.id, {file: "/savepage/content-frame.js", allFrames: true});
Expand Down Expand Up @@ -356,8 +359,8 @@ function configureArchiveTab(node, archiveTab) {
async function configureArchivePage(tab, node) {
if (archiveTabs[tab.id]?.has(tab.url.replace(/#.*$/, ""))) {
await injectCSSFile(tab.id, {file: "ui/edit_toolbar.css"});
await injectScriptFile(tab.id, {file: "lib/jquery.js"});
await injectScriptFile(tab.id, {file: "ui/edit_toolbar.js"});
await injectScriptFile(tab.id, {file: "lib/jquery.js", frameId: 0});
await injectScriptFile(tab.id, {file: "ui/edit_toolbar.js", frameId: 0});

if (settings.open_bookmark_in_active_tab())
node = await Node.getByUUID(tab.url.replace(/^.*#/, "").split(":")[0])
Expand Down
35 changes: 28 additions & 7 deletions addon/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import * as repair from "./core_maintenance.js";
import * as ishell from "./core_ishell.js";
import * as automation from "./core_automation.js";
import * as sync from "./core_sync.js";
import {askCSRPermission} from "./utils_browser.js";

if (_MANIFEST_V3)
import("./mv3_fixup.js");
import("./mv3_persistent.js");

browser.runtime.onInstalled.addListener(async details => {
await settings.load();
Expand All @@ -36,13 +37,21 @@ receiveExternal.startListener(true);
receive.startListener(true);

(async () => {
await systemInitialization;

if (await navigator.storage.persist()) {
// TODO: in MV3 somehow this should be done only once on the addon load
await performStartupInitialization();
await systemInitialization;

if (_MANIFEST_V3) {
// until there is no storage.local API,
// use an alarm as a flag to call initialization function only once
const alarm = await browser.alarms.get("startup-flag-alarm");

console.log("==> core.js loaded");
if (!alarm) {
await performStartupInitialization();
browser.alarms.create("startup-flag-alarm", {periodInMinutes: 525960}); // one year
}
}
else
await performStartupInitialization();
}
})();

Expand All @@ -59,6 +68,8 @@ async function performStartupInitialization() {
sendLocal.performSync();

await UndoManager.commit();

console.log("==> core.js initialized");
}

browser.webRequest.onBeforeSendHeaders.addListener(
Expand Down Expand Up @@ -86,7 +97,15 @@ browser.commands.onCommand.addListener(function(command) {
browser.sidebarAction.open();
}

addBookmark(action);
if (action === "createArchive")
askCSRPermission()
.then(response => {
if (response)
addBookmark(action);
})
.catch(e => console.error(e));
else
addBookmark(action);
});

async function addBookmark(event) {
Expand All @@ -95,3 +114,5 @@ async function addBookmark(event) {

return sendLocal[event]({node: payload});
}

console.log("==> core.js loaded");
2 changes: 1 addition & 1 deletion addon/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.17.4.7",
"version": "1.0",
"applications": {
"gecko": {
"id": "scrapyard@firefox",
Expand Down
9 changes: 4 additions & 5 deletions addon/mv3_fixup.js → addon/mv3_persistent.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// permanently keeps the background page in memory
// works only if blob/http(s) tabs are present
// adapted from https://stackoverflow.com/questions/66618136/persistent-service-worker-in-chrome-extension

let lifeline;

keepAlive();

browser.runtime.onConnect.addListener(port => {
if (port.name === 'keepAlive') {
if (port.name === "keepAlive") {
lifeline = port;
setTimeout(keepAliveForced, 25000); // 25 seconds
port.onDisconnect.addListener(keepAliveForced);
Expand All @@ -23,11 +22,11 @@ function keepAliveForced() {
async function keepAlive() {
if (lifeline) return;

for (const tab of await browser.tabs.query({ url: '*://*/*' })) {
for (const tab of await browser.tabs.query({})) {
try {
await browser.scripting.executeScript({
target: { tabId: tab.id },
func: () => browser.runtime.connect({ name: 'keepAlive' }),
func: () => browser.runtime.connect({ name: "keepAlive" }),
});
browser.tabs.onUpdated.removeListener(retryOnTabUpdate);
return;
Expand All @@ -37,7 +36,7 @@ async function keepAlive() {
}

async function retryOnTabUpdate(tabId, info, tab) {
if (info.url && /^(blob|https?):/.test(info.url)) {
if (info.url && /^(about|blob|https?):/.test(info.url)) {
keepAlive();
}
}
2 changes: 1 addition & 1 deletion addon/savepage/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ function addListeners()
/* Messages from content script */

// Scrapyard //////////////////////////////////////////////////////////////////
case "SAVEPAGE_SETTINGS_CHANGED":
case "savepageSettingsChanged":
initialize(true);
break;
////////////////////////////////////////////////////////////////// Scrapyard //
Expand Down
4 changes: 3 additions & 1 deletion addon/ui/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {send} from "../proxy.js";
import {selectricRefresh, simpleSelectric} from "./shelf_list.js";
import {Query} from "../storage_query.js";
import {systemInitialization} from "../bookmarks_init.js";
import {askCSRPermission} from "../utils_browser.js";

let tree;

Expand Down Expand Up @@ -135,7 +136,8 @@ window.onload = async function () {
});

$("#create-archive").on("click", async e => {
await addBookmark(NODE_TYPE_ARCHIVE);
if (await askCSRPermission())
await addBookmark(NODE_TYPE_ARCHIVE);
window.close();
});

Expand Down
20 changes: 20 additions & 0 deletions addon/utils_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,23 @@ export const CONTEXT_FOREGROUND = 1;
export function getContextType() {
return window.location.pathname === "/background.html"? CONTEXT_BACKGROUND: CONTEXT_FOREGROUND;
}

export async function askCSRPermission() {
if (_MANIFEST_V3)
return browser.permissions.request({origins: ["<all_urls>"]});

return true;
}

export async function hasCSRPermission(verbose = true) {
if (_MANIFEST_V3) {
const response = await browser.permissions.contains({origins: ["<all_urls>"]});

if (!response && verbose)
showNotification("Please, enable optional add-on permissions at the Firefox add-on settings page (about:addons).");

return response;
}

return true;
}

0 comments on commit 8d4a5e5

Please sign in to comment.