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

Safari image pasting may be supported #21

Closed
virtman opened this issue Jan 13, 2016 · 7 comments
Closed

Safari image pasting may be supported #21

virtman opened this issue Jan 13, 2016 · 7 comments

Comments

@virtman
Copy link

virtman commented Jan 13, 2016

Hello,

I have found out, that Safari supports pasted images.
The idea is, Safari reuses same way like you have for FireFox, but unlike FireFox you do not need to postpone image pasting. Pasted images are reported as plain base64 encoded pngs that can be read out + Safari reports the clipboardData.types "image/*****"
Here you can check the basic approach (paste image in Safari into ugly area, I did not make support of it in textarea)
https://fiddle.jshell.net/juwagn/gdar957u/embedded/result/

@layerssss
Copy link
Owner

hi, @virtman nice try, but the reported url ( e.g. webkit-fake-url://e05ae10e-96ae-42d0-a41a-ee03e7c35738/image.tiff ) is not the base64 encoded data, but a id referred to internal store of safari, which is not accessable by javascript. no way we can get the image out of this.

@virtman
Copy link
Author

virtman commented Jan 13, 2016

Hello,
did you check my code on Safari?
Please check it, I know, what you mean, and the code can extract image, I do not read it out like FireFox does, by FireFox we have to postpone pasting and read it out from div with contenteditable, but by Safari you can not read it out from div, but you can read base64 encoded png from clipboard directly.
Here is the snippet with explanation, read the comments, hope, you get enlightment.
///////////////////////
} else { //FireFox and Safari Mac
if(clipboardData.types) {
var strO = false, isImage = false;
var low;
if(clipboardData.types.length > 0) { //text
for(var i = 0; i < clipboardData.types.length; i++) {
if(clipboardData.types[i]) {
low = clipboardData.types[i].toLowerCase();
if(low.indexOf("text/") === 0) { //Safari Mac and FireFox PC handle this correctly
strO = clipboardData.getData('Text');
} else if(low.indexOf("image/") === 0) { //Safari Image workaround
strO = clipboardData.getData('Text'); //here we read the image directly from clipboard as it seems, Safari stores it already png encoded directly in clipboard
isImage = true;
}
}
}
}
if(strO) {
if(ev.preventDefault) { ev.preventDefault(); }
if(!isImage) {
textProcess(el, strO, "FireFox/Safari");
} else {
imageProcess(strO); //by Safari Mac we can handle image directly since extracted from clipboard.
}
} else { //by FireFox we have to postpone the image extraction from div with contenteditable
TEX_TIME[0] = function() {
TEX_TIME[0] = void 0;
checkImages(el);
};
}
}
}

PS: checked on Safari Mac OS X 10.11 (El Capitan)
PPS: as image source I copied via contextmenu some image from safari.

@layerssss
Copy link
Owner

Hi, @virtman thanks for pointing out, but I tried your method and get an empty string (by clipboardData.getData('Text')). Here are my screenshots, so please tell me how you get the image data (since I am running the same platform as yours, latest Safari on El Capitan 10.11.2 ). I have also put my test code into debug branch. I would be nice if you can point the way out on the code.

image
image

@layerssss
Copy link
Owner

Hi, @virtman , just a second after I posted those screenshots I found out that if you directly open the page from Finder ( open it in a file:///Users/xxx/xxx.html url ). You will actually get the image data, paste.js will actually work, maybe the browser considered local filesystem as the same domain as the pasted data. Is that the situation you are actually encountering? If it is, sadly this won't work for any online webpages.

image

@virtman
Copy link
Author

virtman commented Jan 17, 2016

Hello,

well, it seems, only when I copy image from Safari self (as image and not as text) and the source of this image is base64 data:png... (not just link) than it appears in clipboard as text that can be reused.
Also I was to early happy, seems to be only partially supported limited to Safari self as image source.
When copy from other sources I do not get it too.

@virtman
Copy link
Author

virtman commented Jan 17, 2016

It seems Safari developers dislike images from other sources, that is even more evident when trying get around webkit-fake-url.
You can very well handle such image, draw it to canvas, copy such image from canvas to canvas, but as soon you want to get raw data from canvas "infected" by such image via toDataURL or getImageData, you get security message thrown..

@layerssss
Copy link
Owner

Yes, but tainted canvas cannot be exported is not Safari specific. When you draw an image from other domain (using new Image();..). You also get this error. webkit-fake-url is also "an image from other domain", hence not accessable by javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants