CHROME ext / app - 单击图像下载 [英] CHROME ext/app - single click for image download

查看:155
本文介绍了CHROME ext / app - 单击图像下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个简单的Chrome App或Ext来更快下载图片。目前,您必须右键单击并选择另存为等。



我看过这个应用程序接近( https://chrome.google.com/webstore/detail/save-image-to-downloads/ enjefpkmlibebgbbgidmhpmjhcdffhfm?HL = EN )。

清单:

  {
background: {
scripts:[save_to_downloads.js]
},
description:为图片添加一个右键点击直接下载项目,如Safari。,
图标:{
128:icon128.png,
16:icon16.png,
48:icon48.png
},
的键: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXdc9crODeqJcWfwjo671hou / LWYzRmQFi / K / uwGE2Z1ARkm / NAIXS0amsfqCzb2FRJuw9exHGH1E98zotxW94zOY + UesJ4bz9SQT3NTcDcqmB2l1UhHRL0dCXVMig7LZyVyOO8FeBQZULzplF9MylZfBRER + L + d1HN186FFf + 9QIDAQAB,
的manifest_version:2,
的名称: Save Image to Downloads,
permissions:[downloads,contextMenus],
update_url:http://clients2.google.com/service/update2/crx,
version:1.0.5
}

save_to_downloads.js :

 函数_anchorDownloader(url,文件名){
var timeout = 500;
return'javascript:\'<!doctype html>< html>'+
'< head>< / head>'+
'< script> +
'function initDownload(){'+
'var el = document.getElementById(anchor);'+
'el.click();'+
'setTimeout (function(){window.close();},'+ timeout +');'+
'}'+
'< / script>'+
'< body onload =initDownload()>'+
'< a id =anchorhref ='+ url +'download ='+ filename +'>< / a>' +
'< / body>'+
'< / html> \\'';
};

//一个通用的onclick回调函数。
函数downloadResource(info,tab){
var url = info ['srcUrl'];
console.log(url:+ url);
var filename = url.substring(url.lastIndexOf('/')+ 1);
if(chrome.downloads){
chrome.downloads.download({url:url,filename:filename,saveAs:false});
} else {
var a = document.createElement('a');
a.href = url;
a.download =文件名;
// a.click();
chrome.tabs.create({'url':_anchorDownloader(url,filename),'active':false}); //获取下载限制
}
}

//注册上下文菜单
chrome.contextMenus.create({title:Save Image to下载...,上下文:[image],onclick:downloadResource});

这可以使用右键单击和上下文菜单选项下载到默认文件夹。



有谁知道稍微改变它的方法,以便使用鼠标点击(例如ALT +右键单击)来下载而不是上下文菜单?



谢谢

解决方案

您可以使用内容脚本将脚本注入您想要功能的网页:

https:/ /developer.chrome.com/extensions/content_scripts



在您的内容脚本中,您需要添加一个点击事件侦听器,以查看您传递的MouseEvent并查看'altKey'属性是否设置为true。如果是这样,那么你可以做下载。



请注意,许多扩展API只能从扩展自己的页面调用(例如后台页面,浏览器动作弹出窗口等),而不是直接在内容脚本中。因此,在这种情况下,您可能需要做的是使用消息传递API让内容脚本通过要下载的图像的URL将消息发送到您的背景页面。有关更多详细信息,请参阅 https://developer.chrome.com/extensions/messaging

I would like a simple Chrome App or Ext to download images faster. Currently, you have to right click and choose save as, etc.

I have looked at this APP which came close (https://chrome.google.com/webstore/detail/save-image-to-downloads/enjefpkmlibebgbbgidmhpmjhcdffhfm?hl=en).

Manifest:

{
   "background": {
      "scripts": [ "save_to_downloads.js" ]
   },
   "description": "Adds a right-click direct download item for images, like Safari.",
   "icons": {
      "128": "icon128.png",
      "16": "icon16.png",
      "48": "icon48.png"
   },
   "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDXdc9crODeqJcWfwjo671hou/LWYzRmQFi/k/uwGE2Z1ARkm/NAIXS0amsfqCzb2FRJuw9exHGH1E98zotxW94zOY+UesJ4bz9SQT3NTcDcqmB2l1UhHRL0dCXVMig7LZyVyOO8FeBQZULzplF9MylZfBRER+L+d1HN186FFf+9QIDAQAB",
   "manifest_version": 2,
   "name": "Save Image to Downloads",
   "permissions": [ "downloads", "contextMenus" ],
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "1.0.5"
}

save_to_downloads.js:

function _anchorDownloader(url, filename) {
  var timeout = 500;
  return 'javascript:\'<!doctype html><html>'+
    '<head></head>' +
    '<script>' +
      'function initDownload() {'+
        'var el = document.getElementById("anchor");'+
        'el.click();' +
        'setTimeout(function() { window.close(); }, ' + timeout + ');' +
      '}'+
    '</script>' +
    '<body onload="initDownload()">' +
      '<a id="anchor" href="' + url + '" download="'+ filename + '"></a>'+
    '</body>' +
    '</html>\'';
};

// A generic onclick callback function.
function downloadResource(info, tab) {
  var url = info['srcUrl'];
  console.log("url: " + url);
  var filename = url.substring(url.lastIndexOf('/')+1);
  if (chrome.downloads) {
    chrome.downloads.download({ url: url, filename: filename, saveAs: false });
  } else {
    var a = document.createElement('a');
    a.href = url;
    a.download = filename;
    // a.click();
    chrome.tabs.create( { 'url' : _anchorDownloader( url, filename ), 'active' : false  } ); // gets around the download limit
  }
}

// Register the contextual menu
chrome.contextMenus.create({"title": "Save Image to Downloads…", "contexts":["image"], "onclick": downloadResource});

This works using the right click and a context menu option to download to the default folder.

Does anyone know a way to change this slightly so that it uses a mouse click (e.g. ALT+right click) to download instead of the context menu?

Thanks

解决方案

You can do this using a content script to inject script into the pages where you want the functionality:

https://developer.chrome.com/extensions/content_scripts

Inside your content script, you'll want to add an event listener for 'click' that looks at the MouseEvent you get passed and sees if the 'altKey' property is set to true. If so, you can then do the download.

Note that many of the extension APIs are only available to be called from an extensions own pages (eg the background page, browser action popups, etc.) and not directly in the content script. So in this case what you may need to do is use the messaging API to have the content script send a message over to your background page with the URL of the image to download. See https://developer.chrome.com/extensions/messaging for more details.

这篇关于CHROME ext / app - 单击图像下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆