From 08f57b0b951a3deabecad6b1734b1aa8cf67faed Mon Sep 17 00:00:00 2001 From: Guy Khmelnitsky Date: Mon, 11 Mar 2024 10:50:40 +0200 Subject: [PATCH 1/3] Update to manifest v3 --- manifest.json | 15 ++++++++++----- package.json | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/manifest.json b/manifest.json index 0a970bd..e420244 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "__MSG_appName__", "short_name": "__MSG_appShortName__", "description": "__MSG_appDesc__", @@ -15,14 +15,19 @@ "default_icon": "images/icon.png", "default_popup": "popup.html" }, - "web_accessible_resources": [ - "images/icon.png", "images/content_*.png" - ], + "web_accessible_resources": [{ + "resources": [ + "images/icon.png", + "images/content_*.png" + ], + "matches": "*://*/*" + }], "content_scripts": [{ "matches": ["*://www.geni.com/*"], "js": ["jquery.js", "jquery.csv.min.js", "moment.js", "parse-names.js", "shared.js", "content.js"] }], - "permissions": ["activeTab", "storage", "webRequest", "webRequestBlocking", + "permissions": ["activeTab", "storage", "webRequest", "webRequestBlocking"], + "host_permissions": [ "*://*.geni.com/", "*://*.findagrave.com/", "*://*.familysearch.org/", diff --git a/package.json b/package.json index 9ac1271..ad79580 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,6 @@ "dependencies": { "jquery": "^3.3.1", "moment": "^2.29.3", - "jquery.csv": "v1.0.21" + "jquery-csv": "v1.0.21" } } \ No newline at end of file From 47c05c4d1fea1b5f15d66d691fd34b1f3c61c011 Mon Sep 17 00:00:00 2001 From: Guy Khmelnitsky Date: Mon, 11 Mar 2024 12:30:12 +0200 Subject: [PATCH 2/3] Try fixing `service_worker` --- service_worker.js | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 service_worker.js diff --git a/service_worker.js b/service_worker.js new file mode 100644 index 0000000..7f7a40a --- /dev/null +++ b/service_worker.js @@ -0,0 +1,134 @@ + +/** + * Possible parameters for request: + * action: "xhttp" for a cross-origin HTTP request + * method: Default "GET" + * url : required, but not validated + * data : data to send in a POST request + * variable : pass private variable into the callback + * + * The callback function is called upon completion of the request + * https://stackoverflow.com/questions/7699615/cross-domain-xmlhttprequest-using-background-pages + * + * Call to verify HistoryLink authentication to Geni & query Family Data + * */ +function getUrlFromJson(obj) { + let result = ""; + for (let key in obj) { + if (result !== "") { + result += "&"; + } + result += key + "=" + encodeURIComponent(obj[key]); + } + return result; +} + +function getJsonFromUrl(query) { + const result = {}; + query.split("&").forEach(function(part) { + const item = part.split("="); + result[item[0]] = decodeURIComponent(item[1]); + }); + return result; +} + +chrome.runtime.onMessage.addListener(function(request, sender, callback) { + if (request.action === "xhttp") { + let method = request.method ? request.method.toUpperCase() : 'GET'; + + let requestInit = { + method: method + } + + let fetch_res = null; + if (method === 'POST') { + requestInit.body = request.data; + requestInit.headers = {"Content-Type": "application/x-www-form-urlencoded"}; + + data = getJsonFromUrl(request.data) + if (data.photo !== undefined) { + fetch_res = fetch(data.photo).then(response => { + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + return response.text(); // Or response.json() for JSON data + }).then(response => { + let binary = "" + for(let i=0;i { + const valrtn = {error: error, responseURL: data.photo}; + callback(valrtn); + }) + } + } + + + if (!fetch_res) { + fetch_res = fetch(request.url, { + method, + // Pass headers for POST requests + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + body: request.method === 'POST' ? request.data : null, + }); + } + + fetch_res.then(response => { + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + return response.text(); // Or response.json() for JSON data + }).then(responseText => { + const valrtn = {source: responseText, variable: request.variable, responseURL: response}; + callback(valrtn); + }).catch(error => { + const valrtn = { error: error, variable: request.variable, responseURL: error.response?.url || null, // Get URL from error if available + }; + callback(valrtn); + }).then(() => { + return true; + }); + + // return true; // prevents the callback from being called too early on return + } else if (request.action === "icon") { + chrome.action.setIcon({path: request.path}); + return true; + } +}); + +const iframeHosts = [ + 'geni.com', +]; +chrome.runtime.onInstalled.addListener(() => { + const RULE = { + id: 1, + condition: { + initiatorDomains: [chrome.runtime.id], + requestDomains: iframeHosts, + resourceTypes: ['sub_frame'], + }, + action: { + type: 'modifyHeaders', + responseHeaders: [ + {header: 'X-Frame-Options', operation: 'remove'}, + {header: 'Frame-Options', operation: 'remove'}, + // Uncomment the following line to suppress `frame-ancestors` error + {header: 'Content-Security-Policy', operation: 'remove'}, + ], + }, + }; + chrome.declarativeNetRequest.updateDynamicRules({ + removeRuleIds: [RULE.id], + addRules: [RULE], + }); +}); \ No newline at end of file From f51bdc7b60a8196bccc9f49b30d3a1f319aa51d0 Mon Sep 17 00:00:00 2001 From: Guy Khmelnitsky Date: Mon, 11 Mar 2024 12:30:28 +0200 Subject: [PATCH 3/3] Use `service_worker` --- manifest.json | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/manifest.json b/manifest.json index e420244..bbad01a 100644 --- a/manifest.json +++ b/manifest.json @@ -5,13 +5,15 @@ "description": "__MSG_appDesc__", "default_locale": "en", "version": "4.10.23", + "minimum_chrome_version": "101", "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" }, "background": { - "scripts": ["background.js"] + "service_worker": "service_worker.js", + "type": "module" }, - "browser_action": { + "action": { "default_icon": "images/icon.png", "default_popup": "popup.html" }, @@ -20,13 +22,13 @@ "images/icon.png", "images/content_*.png" ], - "matches": "*://*/*" + "matches": ["*://*/*"] }], "content_scripts": [{ "matches": ["*://www.geni.com/*"], "js": ["jquery.js", "jquery.csv.min.js", "moment.js", "parse-names.js", "shared.js", "content.js"] }], - "permissions": ["activeTab", "storage", "webRequest", "webRequestBlocking"], + "permissions": ["activeTab", "storage", "webRequest", "declarativeNetRequestWithHostAccess"], "host_permissions": [ "*://*.geni.com/", "*://*.findagrave.com/",