Skip to content

Instantly share code, notes, and snippets.

@nmu
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmu/6d1cdb57e4d34f45af23 to your computer and use it in GitHub Desktop.
Save nmu/6d1cdb57e4d34f45af23 to your computer and use it in GitHub Desktop.
Save all attachments from Basecamp message body.
/*
* 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