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

add Improved Image Support #44

Merged
merged 3 commits into from
Oct 17, 2022
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
- Crop Image
- Photo Editor (Adjust, Draw, Watermark, Filters, Finetune, Resize, Export As PNG, JPEG, JPG, WEBP)
- Download Image
- Upload Image to imgBB
- Upload Image to imgBB or imgur
- Color picker
- Image Details
- Change background color (Dark / Light / Blurred)
Expand Down
61 changes: 44 additions & 17 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ let welcomeUrl = "https://betterviewer.surge.sh/welcome.html";

// when tab is created or reloaded
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {



if (changeInfo.status == 'loading') {
// check if tab is marked as injected
if (InjectedTabs.includes(tabId)) {
Expand Down Expand Up @@ -48,12 +45,8 @@ chrome.webRequest.onHeadersReceived.addListener(function (details) {
let contentDispositionHeader = getHeaderFromHeaders(details.responseHeaders, 'content-disposition');
let isContentDispositionAttachment = contentDispositionHeader && contentDispositionHeader.value.toLowerCase().includes('attachment');



let res = header && header.value.split(';', 1)[0];



if (res.indexOf('image') === -1) {
// remove from injected list
InjectedTabs = InjectedTabs.filter(function (item) {
Expand All @@ -68,31 +61,64 @@ chrome.webRequest.onHeadersReceived.addListener(function (details) {
// add tab to injected list
InjectedTabs.push(details.tabId);

// clear 'content-security-policy'
for (let respHeader of details.responseHeaders) {
if (respHeader.name.toLowerCase() === 'content-security-policy') {
respHeader.value = '';
}
}

// Remove "content-security-policy" header from the selected image to allow it to be loaded in the viewer
// Same idea from here : https://github.com/PhilGrayson/chrome-csp-disable/blob/master/background.js
// for (let i = 0; i < details.responseHeaders.length; i++) {
// if (details.responseHeaders[i].name.toLowerCase() === 'content-security-policy') {
// details.responseHeaders[i].value = '';
// }
// }
return {
responseHeaders: details.responseHeaders
};
}
}
}, {
urls: ['<all_urls>'],
types: ['main_frame']
}, ['responseHeaders', 'blocking']);

// run .onCompleted for local files
chrome.webRequest.onCompleted.addListener(function (details) {
if (details.tabId !== -1) {
let header = getHeaderFromHeaders(details.responseHeaders, 'content-type');

// check if content-disposition is attachment, if it is, do not inject
let contentDispositionHeader = getHeaderFromHeaders(details.responseHeaders, 'content-disposition');
let isContentDispositionAttachment = contentDispositionHeader && contentDispositionHeader.value.toLowerCase().includes('attachment');

let res = header && header.value.split(';', 1)[0];

if (res.indexOf('image') === -1) {
// remove from injected list
InjectedTabs = InjectedTabs.filter(function (item) {
return item !== details.tabId;
});
}

// check if image
if (res && res.indexOf('image') !== -1 && InjectedTabs.indexOf(details.tabId) === -1 && !isContentDispositionAttachment) {

console.log(details);
// add tab to injected list
InjectedTabs.push(details.tabId);

// clear 'content-security-policy'
for (let respHeader of details.responseHeaders) {
if (respHeader.name.toLowerCase() === 'content-security-policy') {
respHeader.value = '';
}
}


return {
responseHeaders: details.responseHeaders
};
}
}
}, {
urls: ['*://*/*'],
urls: ['file://*'],
types: ['main_frame']
}, ['responseHeaders', 'blocking']);
}, ['responseHeaders']);


// when the extension is installed or upgraded ...
Expand Down Expand Up @@ -184,6 +210,7 @@ function set_default_settings(){
exit: true,
about: true,
zoom_ratio: 0.1, // 0.1 is +/- 10% Zoom, 0.5 is +/- 50% Zoom, etc...
upload_site: "imgbb", // imgbb, imgur
notification_gravity: "top", // top, bottom
notification_position: "right", // left, right
toolbar_position: "bottom", // top, bottom
Expand Down
15 changes: 8 additions & 7 deletions css/viewer.min.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@
overflow: hidden;
position: absolute;
right : 0;
top : 0
top : 0;
cursor: grab;
cursor: -webkit-grab;
}

.viewer-canvas:active {
cursor: grabbing;
cursor: -webkit-grabbing;
}

.viewer-canvas>img {
Expand Down Expand Up @@ -394,12 +401,6 @@
visibility: hidden
}

.viewer-move {
cursor: move;
cursor: -webkit-grab;
cursor: grab
}

.viewer-fade {
opacity: 0
}
Expand Down
Loading