Last active
August 29, 2015 14:08
-
-
Save nmu/6d1cdb57e4d34f45af23 to your computer and use it in GitHub Desktop.
Save all attachments from Basecamp message body.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Saves from URLs like https://basecamp.com/{{accountId?}}/projects/{{projectId?}}/messages/{{messageId}} | |
* Assumptions (true as of 2014-11-07): | |
* - jQuery available as global $ | |
* - sufficient selector assumes attachment links have attribute 'data-attachment-id' | |
*/ | |
function save (url, options) { | |
options || (options = {}); | |
var filename, | |
saver, | |
saveEvent; | |
// Try to get filename from options, generative, or fallback | |
filename = options.as || options.filename || url.split("/").pop() || "meow"; | |
// Make a fake link for saving | |
saver = document.createElement('a'); | |
saver.href = url; | |
saver.target = "_blank"; | |
saver.download = filename; | |
// bind and trigger a fake click event | |
saveEvent = document.createEvent('Event'); | |
saveEvent.initEvent('click', true, true); | |
saver.dispatchEvent(saveEvent); | |
// Release the URL reference after saving | |
window.URL.revokeObjectURL(saver.href); | |
}; | |
$('a[data-attachment-id]').each(function (idx, item) { item.href && save(item.href); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment