有没有办法检测正在下载的特定文件是否是Gmail附件? 我正在寻找一种方法来编写一个Greasemonkey脚本,它可以帮助我根据下载源组织下载,说Gmail电子邮件附件与其他内容有不同的行为。
到目前为止,我发现附件会重定向到https://mail-attachment.googleusercontent.com/attachment/u/0/
,我认为这还不够。
修改
由于附加组件比用户脚本更强大,我决定追求Add On的想法。但是,检测问题仍未解决。
答案 0 :(得分:2)
这对于一个问题来说太复杂了;它至少有这些主要部分:
一旦您的脚本识别出相应的文件URL和/或链接(打开一个新问题以获得更多帮助,并包含您想要的页面类型和链接的图片。),它可以与Firefox添加 - 如下所示,自动保存这些文件。
借助其他附加组件自动保存Greasemonkey中的文件:
如果您使用加载项构建器或SDK来安装或“测试”the DANGER. DANGER. DANGER. File download utility,
然后你可以使用像这样的Greasemonkey脚本来自动保存文件:
// ==UserScript==
// @name _Call our File download add-on to trigger a file download.
// @include https://mail.google.com/mail/*
// @include https://stackoverflow.com/questions/14440362/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
var fileURL = "http://userscripts.org/scripts/source/29222.user.js";
var savePath = "D:\\temp\\";
var extensionLoaded = false;
window.addEventListener ("ImAlivefromExtension", function (zEvent) {
console.log ("The test extension appears to be loaded!", zEvent.detail);
extensionLoaded = true;
} );
window.addEventListener ("ReplyToDownloadRequest", function (zEvent) {
//var xxxx = JSON.parse (zEvent.detail);
console.log ("Extension replied: ", zEvent.detail);
} );
$("body").prepend ('<button id="gmFileDownloadBtn">Click to File download request.</button>');
$("#gmFileDownloadBtn").click ( function () {
if (extensionLoaded) {
detailVal = JSON.stringify (
{targFileURL: fileURL, targSavePath: savePath}
);
var zEvent = new CustomEvent (
"SuicidalDownloadRequestToAddOn",
{"detail": detailVal }
);
window.dispatchEvent (zEvent);
}
else {
alert ("The file download extension is not loaded!");
}
} );
您可以在this SO question page上测试脚本。
请注意,任何其他扩展程序,用户脚本,网页或插件都可以侦听或发送欺骗事件,目前唯一的安全措施是限制扩展程序运行的页面。
供参考,扩展源文件如下。其余部分由Firefox的附加SDK提供。
内容脚本:
var zEvent = new CustomEvent ("ImAlivefromExtension",
{"detail": "GM, DANGER, DANGER, DANGER, File download utility" }
);
window.dispatchEvent (zEvent)
window.addEventListener ("SuicidalDownloadRequestToAddOn", function (zEvent) {
console.log ("Extension received download request: ", zEvent.detail);
//-- Relay request to extension main.js
self.port.emit ("SuicidalDownloadRequestRelayed", zEvent.detail);
//-- Reply back to GM, or whoever is pretending to be GM.
var zEvent = new CustomEvent ("ReplyToDownloadRequest",
{"detail": "Your funeral!" }
);
window.dispatchEvent (zEvent)
} );
背景JS:
//--- For security, MAKE THESE AS RESTRICTIVE AS POSSIBLE!
const includePattern = [
'https://mail.google.com/mail/*',
'https://stackoverflow.com/questions/14440362/*'
];
let {Cc, Cu, Ci} = require ("chrome");
Cu.import ("resource://gre/modules/Services.jsm");
Cu.import ("resource://gre/modules/XPCOMUtils.jsm");
Cu.import ("resource://gre/modules/FileUtils.jsm");
let data = require ("sdk/self").data;
let pageMod = require ('sdk/page-mod');
let dlManageWindow = Cc['@mozilla.org/download-manager-ui;1'].getService (Ci.nsIDownloadManagerUI);
let fileURL = "";
let savePath = "";
let activeWindow = Services.wm.getMostRecentWindow ("navigator:browser");
let mod = pageMod.PageMod ( {
include: includePattern,
contentScriptWhen: 'end',
contentScriptFile: [ data.url ('ContentScript.js') ],
onAttach: function (worker) {
console.log ('DANGER download utility attached to: ' + worker.tab.url);
worker.port.on ('SuicidalDownloadRequestRelayed', function (message) {
var detailVal = JSON.parse (message);
fileURL = detailVal.targFileURL;
savePath = detailVal.targSavePath;
console.log ("Received request to \ndownload: ", fileURL, "\nto:", savePath);
downloadFile (fileURL, savePath);
} );
}
} );
function downloadFile (fileURL, savePath) {
dlManageWindow.show (activeWindow, 1);
try {
let newFile;
let fileURIToDownload = Services.io.newURI (fileURL, null, null);
let persistWin = Cc['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
.createInstance (Ci.nsIWebBrowserPersist);
let fileName = fileURIToDownload.path.slice (fileURIToDownload.path.lastIndexOf ('/') + 1);
let fileObj = new FileUtils.File (savePath);
fileObj.append (fileName);
if (fileObj.exists ()) {
console.error ('*** Error! File "' + fileName + '" already exists!');
}
else {
let newFile = Services.io.newFileURI (fileObj);
let newDownload = Services.downloads.addDownload (
0, fileURIToDownload, newFile, fileName, null, null, null, persistWin, false
);
persistWin.progressListener = newDownload;
persistWin.savePrivacyAwareURI (fileURIToDownload, null, null, null, "", newFile, false);
}
} catch (exception) {
console.error ("Error saving the file! ", exception);
dump (exception);
}
}
答案 1 :(得分:0)
到目前为止,您所说的唯一可以做的就是制作插件(Firefox)和扩展(如果您愿意,可以使用扩展程序)。
如果您仔细查看附件下载,则会在以下情况下发生:
1)您点击附件图标
2)如果您点击下载
对于这两件事,您可以找到包含<a>
值的download_url
标记的点击事件。您可以使用js / jquery轻松完成创建扩展程序。
因此,当用户尝试下载附件时,您可以编写该功能。
答案 2 :(得分:0)
您可以使用Gmail上下文小工具修改Google方面的行为:
上下文小工具无法直接访问附件,但服务器方面,您可以使用IMAP访问附件(基于小工具识别的Gmail邮件ID):
使用小工具和服务器端IMAP具有与浏览器无关的优势。
对于下载的Gmail附件而不是任何给定的下载而言,您想要做的事情并不完全清楚(将其保存到其他位置?对附件数据执行操作?)但是上下文小工具和IMAP应该会给您一些在浏览器下载开始之前根据需要修改附件数据的机会。