-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserscript.js
102 lines (82 loc) · 2.84 KB
/
userscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// ==UserScript==
// @name Download images from vsco.co
// @namespace http://www.dieterholvoet.com
// @version 1.2.8
// @description Adds download buttons for the full-resolution images to thumbnails all over vsco.co
// @author Dieter Holvoet
// @match *://vsco.co/*
// @grant GM_download
// @grant GM_info
// @grant GM_notification
// @grant GM_getResourceText
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js
// @require https://bowercdn.net/c/jquery-observe-2.0.2/jquery-observe.js
// @resource styles https://raw.githubusercontent.com/DieterHolvoet/userscript-vsco-downloader/master/styles.css
// ==/UserScript==
(function () {
'use strict';
var itemSelectors = '.MediaImage, .article-image';
$(document.head).append(
$('<style />').text(GM_getResourceText('styles'))
);
$(document.body).observe('childList subtree', handleSubtreeChange);
$(document.body).ready(handleDocumentReady);
function handleDocumentReady(e) {
$(itemSelectors).each(function (i, $item) {
appendButton($item);
});
}
function handleSubtreeChange(e) {
e.addedNodes.forEach(function (addedNode) {
if ($(addedNode).is(itemSelectors)) {
appendButton(addedNode);
} else {
$(addedNode).find(itemSelectors).each(function (i, item) {
appendButton(item);
});
}
});
}
function appendButton(item) {
if ($(item).find('.dl-btn').length) {
return;
}
var $img = $(item).find('img');
var url = $img.attr('src');
$img.parent()
.addClass('relative')
.append(makeButton(url));
}
function download(url) {
url = url.split('?')[0];
if (!url.startsWith('http')) {
url = window.location.protocol + (url.startsWith('//') ? '' : '//') + url;
}
var fileNameParts = url.split('/');
var fileName = fileNameParts[fileNameParts.length - 1];
var options = {
url: url,
name: fileName,
onerror: function (e) {
logError('Download failed. Reason: ' + e.error);
}
};
GM_download(options);
}
function logError(message) {
var details = {
title: GM_info.script.name,
text: message,
};
GM_notification(details);
console.error('%c' + GM_info.script.name + '%c: ' + message, 'font-weight: bold', 'font-weight: normal');
}
function makeButton(url) {
var $btn = $('<button class="dl-btn">Download</button>');
$btn.on('click', function (e) {
e.preventDefault();
download(url);
});
return $btn;
}
})();