Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move SmartCopy to Chrome Manifest V3 #112

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "__MSG_appName__",
"short_name": "__MSG_appShortName__",
"description": "__MSG_appDesc__",
"default_locale": "en",
"minimum_chrome_version": "101",
"version": "4.10.24",
"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"
},
"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", "declarativeNetRequestWithHostAccess"],
"host_permissions": [
"*://*.geni.com/",
"*://*.findagrave.com/",
"*://*.familysearch.org/",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"dependencies": {
"jquery": "^3.3.1",
"moment": "^2.29.3",
"jquery.csv": "v1.0.21"
"jquery-csv": "v1.0.21"
}
}
134 changes: 134 additions & 0 deletions service_worker.js
Original file line number Diff line number Diff line change
@@ -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<response.length;i++){
binary += String.fromCharCode(response.charCodeAt(i) & 0xff);
}
delete data.photo
data.file = btoa(binary)
return fetch(getUrlFromJson(data), {method: method,
headers: { "Content-Type": "application/x-www-form-urlencoded",},
body: request.data});
}).catch(error => {
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],
});
});