Skip to content

Commit

Permalink
commenting and a little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
StigNygaard committed Sep 25, 2023
1 parent 99711e8 commit 191f457
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 58 deletions.
47 changes: 31 additions & 16 deletions WebExtension/backgroundscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
frameId: info.frameId, // related to globalThis/window/frames ?
frameUrl: info.frameUrl
}
);
)
.then ((r) => {
// console.log(`xIFr: ${r}`)
})
.catch((e) => {
console.error('xIFr: Sending parseImage message failed! \n' + e)
});
}
)
.catch((err) => {
Expand All @@ -160,18 +166,27 @@ browser.contextMenus.onClicked.addListener((info, tab) => {
sessionStorage.set("winpop", options.popupPos)
.then(
() => {
browser.tabs.sendMessage(tab.id, {
message: "parseImage",
imageURL: info.srcUrl,
mediaType: info.mediaType,
targetId: info.targetElementId,
supportsDeepSearch: !!info.targetElementId, // "deep-search" supported in Firefox 63+
goDeepSearch: !!info.targetElementId && !options.devDisableDeepSearch,
supportsDeepSearchModifier: !!info.modifiers,
deepSearchBigger: !!info.modifiers?.includes("Shift"),
deepSearchBiggerLimit: options.deepSearchBiggerLimit, fetchMode : options.devFetchMode,
frameId: info.frameId, // related to globalThis/window/frames ?
frameUrl: info.frameUrl
browser.tabs.sendMessage(
tab.id,
{
message: "parseImage",
imageURL: info.srcUrl,
mediaType: info.mediaType,
targetId: info.targetElementId,
supportsDeepSearch: !!info.targetElementId, // "deep-search" supported in Firefox 63+
goDeepSearch: !!info.targetElementId && !options.devDisableDeepSearch,
supportsDeepSearchModifier: !!info.modifiers,
deepSearchBigger: !!info.modifiers?.includes("Shift"),
deepSearchBiggerLimit: options.deepSearchBiggerLimit, fetchMode : options.devFetchMode,
frameId: info.frameId, // related to globalThis/window/frames ?
frameUrl: info.frameUrl
}
)
.then ((r) => {
// console.log(`xIFr: ${r}`)
})
.catch((e) => {
console.error('xIFr: Sending parseImage message failed! \n' + e)
});
}
);
Expand Down Expand Up @@ -259,7 +274,7 @@ browser.runtime.onMessage.addListener(
}
)
.catch(fetchdata_error);
return true;
return true; // Tell it to expect a later response (to be sent with sendResponse())

} else if (message.message === "fetchdataBase64") { // Probably a Chromium browser

Expand Down Expand Up @@ -291,7 +306,7 @@ browser.runtime.onMessage.addListener(
}
)
.catch(fetchdata_error);
return true;
return true; // Tell it to expect a later response (to be sent with sendResponse())

}

Expand Down Expand Up @@ -352,7 +367,7 @@ browser.runtime.onMessage.addListener(
sendResponse(data);
}
);
return true;
return true; // tell it to expect a later response (sent with sendResponse())

}

Expand Down
1 change: 1 addition & 0 deletions WebExtension/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@
console.error('xIFr: No image detected in simple search.');
}
}
return Promise.resolve(`The contentscript says thanks for the '${request.message}' message! 😊`);
});
globalThis.contentListenerAdded = true;
}
Expand Down
86 changes: 44 additions & 42 deletions WebExtension/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function populate(response) {
}
return {width: w + 'px', height: h + 'px'};
}
// console.log('xIFr: POPUP with \n' + JSON.stringify(response));
if (response.properties.URL) {
const image = document.querySelector("#image img");
if (response.properties.naturalWidth) {
Expand Down Expand Up @@ -305,39 +306,6 @@ function populate(response) {
const link = createRichElement('a', {href: url}, letter);
return createRichElement('div', {title: title, class: className}, link);
}
function openOptions(event) {
if (event) {
event.stopPropagation();
event.preventDefault();
}
browser.runtime.openOptionsPage();
self.close();
}
function copyPasteContent() {
let s = 'FILE PROPERTIES\n\n';
s += document.getElementById('properties').innerText + '\n\n';
const rows = document.querySelectorAll('table#data tr');
if (rows && rows.length > 0) {
document.body.classList.add("copypastemode");
s += 'IMAGE META DATA\n\n';
rows.forEach((row) => {
const tds = row.getElementsByTagName('td');
if (tds && tds.length > 1) {
s += tds[0].innerText + ': ' + tds[1].innerText + '\n';
}
});
document.body.classList.remove("copypastemode");
}
return s;
}
function copyToClipboard(event) {
if (event) {
event.stopPropagation();
event.preventDefault();
}
// Copy to clipboard
navigator.clipboard.writeText(copyPasteContent());
}

const orderedKeys = ["Headline", "Caption", "ObjectName", "Date", "Creditline", "Copyright", "UsageTerms", "LicenseURL",
"Creator", "CreatorAddress", "CreatorCity", "CreatorRegion", "CreatorPostalCode", "CreatorCountry", "CreatorPhoneNumbers", "CreatorEmails", "CreatorURLs",
Expand Down Expand Up @@ -386,16 +354,40 @@ function populate(response) {
self.close();
}, true)
});
document.getElementById("settings").addEventListener('click', openOptions, true);
keyShortcuts.register("o", openOptions);
keyShortcuts.register("O", openOptions);
if (navigator.clipboard?.writeText) {
document.getElementById("cpClipboard").addEventListener('click', copyToClipboard, true);
keyShortcuts.register("c", copyToClipboard);
keyShortcuts.register("C", copyToClipboard);
} else {
document.body.classList.add('copyUnsupported'); // Hide copy button
}

function openOptions(event) {
if (event) {
event.stopPropagation();
event.preventDefault();
}
browser.runtime.openOptionsPage();
self.close();
}
function copyPasteContent() {
let s = 'FILE PROPERTIES\n\n';
s += document.getElementById('properties').innerText + '\n\n';
const rows = document.querySelectorAll('table#data tr');
if (rows?.length) {
document.body.classList.add("copypastemode");
s += 'IMAGE META DATA\n\n';
rows.forEach((row) => {
const tds = row.getElementsByTagName('td');
if (tds && tds.length > 1) {
s += tds[0].innerText + ': ' + tds[1].innerText + '\n';
}
});
document.body.classList.remove("copypastemode");
}
return s;
}
function copyToClipboard(event) {
if (event) {
event.stopPropagation();
event.preventDefault();
}
// Copy to clipboard
navigator.clipboard.writeText(copyPasteContent());
}
function setup(options) {
if (context.prefersDark(options["dispMode"])) {
Expand All @@ -411,6 +403,16 @@ function setup(options) {
document.body.classList.add("show" + v);
}
});
document.getElementById("settings").addEventListener('click', openOptions, true);
keyShortcuts.register("o", openOptions);
keyShortcuts.register("O", openOptions);
if (navigator.clipboard?.writeText) {
document.getElementById("cpClipboard").addEventListener('click', copyToClipboard, true);
keyShortcuts.register("c", copyToClipboard);
keyShortcuts.register("C", copyToClipboard);
} else {
document.body.classList.add('copyUnsupported'); // Hide copy button
}
}
function init() {
context.getOptions().then(setup);
Expand Down

0 comments on commit 191f457

Please sign in to comment.