简体   繁体   English

如何访问webkit浏览器中的粘贴文件? (作为Google Chrome)

[英]How can I access pasted file in webkit browsers? (as Google Chrome)

It would be very convenient if one was able to paste images here, on Stack Exchange instead of meddling with a file dialog. 如果能够在Stack Exchange上粘贴图像而不是插入文件对话框,那将是非常方便的。 Similar feature was (is?) implemented here, but only for Webkit browsers . 这里实现了类似的功能(是?),但仅适用于Webkit浏览器

I was developing userscript that does that . 我正在开发那样做的用户脚本 Funny thing is that I never accomplished to get the file (which is different from raw image data) from clipboard in Webkit browsers, while in Firefox it works. 有趣的是,我从未完成从Webkit浏览器中的剪贴板获取文件 (与原始图像数据不同),而在Firefox中它可以工作。

Firefox solution: Firefox解决方案:

  div.addEventListener('paste', function(event){
    //I'm actually not sure what should event.originalEvent be. I copypasted this
    var items = (event.clipboardData || event.originalEvent.clipboardData);
    console.log("paste", items);
    //Try to get a file and handle it as Blob/File
    var files = items.items || items.files;
    if(files.length>0) {  
      //Being lazy I just pick first file
      var file = files[0];
      //handle the File object
      _this.processFile(file);

      event.preventDefault();
      event.cancelBubble = true;
      return false;
    }
  });

Before Chrome doesn't have as nice documentation as Firefox has (I mean MDN), I tried to inspect what's going on. 在Chrome没有像Firefox那样好的文档(我的意思是MDN)之前,我试图检查发生了什么。 I copied a file and I pasted it in Google chrome (v39). 我复制了一个文件并将其粘贴到Google Chrome(v39)中。 This is what I get in the DataTransfer object in the console: 这是我在控制台中的DataTransfer对象中得到的:

粘贴事件Google Chrome

For refference, here's the same event in Firefox: 对于参考,这是Firefox中的相同事件:

粘贴事件文件firefox

The other two arrays, items and types are not present in Firefox implementation. Firefox实现中不存在另外两个数组, itemstypes When I copy text or raw image data Chrome represents it as DataTransferItem . 当我复制文本或原始图像数据时,Chrome将其表示为DataTransferItem I figured out that it's best to ignore those: 我发现最好忽略这些:

  //Being lazy I just pick first file
  var file = files[0];
  //GOOGLE CHROME
  if(file.getAsFile) {
    console.log("DataTransferItem:", file);
    //HTML or text will be handled by contenteditable div
    if(file.type!="text/plain" && file.type!="text/html") {
      //handle the File object
      _this.processFile(file.getAsFile());
    }
  }
  else 
    ...

So far, I never occurred to get anything else than text/plain or text/html . 到目前为止,除了text/plaintext/html之外,我从未想过得到任何其他东西。 For these, .getAsFile returns null . 对于这些, .getAsFile返回null

I find the google chrome model a little confusing. 我觉得google chrome模型有点令人困惑。 It gives you more info about raw data (text/raw image), which can only be accessed using content editable div, but isn't very clear to me. 它为您提供了有关原始数据(文本/原始图像)的更多信息,这些信息只能使用内容可编辑的div进行访问,但对我来说并不是很清楚。

Our magic sauce is just: 我们的神奇酱油只是:

$('body').bind('paste', handlepaste);

Where handlepaste is very similar to yours, so should work for you just fine for Chrome. handlepaste与你的非常相似,所以对你来说也适合Chrome。

Unfortunately the above completely fails for FF, and we are loath to add a contenteditable anywhere (in particular given that it has to be in focus for this to work and we don't want to steal the focus). 不幸的是,上面的FF完全失败了,我们不愿意在任何地方添加一个contenteditable (特别是考虑到它必须成为焦点才能使其工作,我们不想窃取焦点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JavaScript从WebKit浏览器访问本地文件系统? - How to access local file system from WebKit browsers using JavaScript? 如何在Webkit浏览器中绘制黑色阴影? - How can I draw a black shadow in webkit browsers? 在Google Chrome和其他WebKit浏览器中,点击事件处理程序变慢 - Click event handler slow in Google Chrome and other WebKit browsers 如何在离开页面之前(在Webkit浏览器中)强制脚本完成操作? - How can I force scripts to finish before leaving the page (in webkit browsers)? 如何在 JavaScript 中访问粘贴文件的文件名(而不仅仅是数据)? - How do I access the file name (and not just the data) of a pasted file in JavaScript? 如何向Chrome / Chromium浏览器添加一些内容? - How can I add some content to Chrome/Chromium browsers? Settimeout适用于基于chrome / webkit的浏览器,但不适用于firefox - Settimeout works in chrome/webkit based browsers but not firefox 我无法在Google Chrome中读取Cookie,但可以在所有其他浏览器中读取 - I cant read my cookie out in google chrome but I can in all other browsers Google使用Webkit浏览器映射地理位置错误 - Google maps geolocation bug with webkit browsers 如何确定Webkit Chrome和Safari浏览器的图像高度和宽度? - How do you determine an image height and width for webkit Chrome and Safari browsers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM