Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- let src = data.element.mediaInfo && data.element.mediaInfo.source;
- if (!src) {
- setStatus('Quick Save: No media found');
- return;
- }
- // var domain = data.context.frameUrl.replace(/^(https?:\/\/)?([^@\n]+@)?(www\.)?(?:[^:\/\n?]+)/ig, '');
- var matches = data.context.frameUrl.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
- var domain = matches && matches[1];
- domain = domain.replace(/[:/\\?!#*"<>]/ig, '');
- // var domain = data.context.frameUrl.replace(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/ig, '')[1];
- // domain: /^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n?]+)/i
- // var fullurl = encodeURIComponent(data.context.frameUrl) + "/";
- // var pagetitle = document.title.replace(/[^a-zA-Z0-9-_]/g, '_');
- var pagetitle = document.title.replace(/[:/\\?!#*"<>|]/ig, '_');
- pagetitle = pagetitle.replace(/(^\([0-9]+\) )/g, '');
- pagetitle = pagetitle.replace(/ /g, '_');
- pagetitle = pagetitle.substr(0, 60);
- let placeholders = [], rules = [];
- // -- Define placeholders --
- placeholders.push([ '%MEDIADIR%', 'inbox/' + domain + "/" + pagetitle + "/"]);
- placeholders.push([ '%MISC%', 'misc/' + domain + "/" + pagetitle + "/"]);
- // placeholders.push([ '%AUDIO_DIR%', 'Audio/' ]);
- // -- Define rules --
- // Capturing group 1 must be the filename.
- // Defaults
- rules.push({ name: 'MEDIA', regex: /([^\/]+\.(gif|png|jpe?g|mp4|mkv|webm|flac|mp3|m4a|aac))($|\?|#)/i, target: '%MEDIADIR%/%F%' });
- rules.push({ name: 'MISC', regex: /(?=\w+\.\w{3,4}$).+/i, target: '%MISC%/%F%' });
- //rules.push({ name: 'MISC2', regex:/([^\/]+)($|\?|#)/i, target: '%MISC%/%F%' });
- // rules.push({ name: 'Video', regex: /([^\/]+\.(webm|mp4|mkv))($|\?|#)/i, target: '%VIDEO_DIR%/%F%' });
- // rules.push({ name: 'Audio', regex: /([^\/]+\.(mp3|flac|m4a|aac))($|\?|#)/i, target: '%AUDIO_DIR%/%F%' });
- // Evaluate rules
- let match = null;
- for (let i = 0; i < rules.length; i++) {
- if ((match = src.match(rules[i].regex)) != null) {
- // Evaluate placeholders
- let filename = match[1];
- placeholders.push([ '%F%', filename ]);
- let target = placeholders.reduce((target, placeholder) => target.replace(placeholder[0], placeholder[1]), rules[i].target);
- // data.context.frameUrl+"/"
- // -- Privileged operation begin --
- return executeInBackground((rule, src, target) => {
- // Start the download
- return browser.downloads.download({ url: src, filename: target }).then(id => {
- if (rule.erase) {
- // Remove the download from history on completion
- let listener = (downloadDelta) => {
- if ((downloadDelta.id === id) && (downloadDelta.state.current !== 'in_progress')) {
- browser.downloads.onChanged.removeListener(listener);
- browser.downloads.erase({ id: downloadDelta.id });
- }
- };
- browser.downloads.onChanged.addListener(listener);
- }
- });
- }, [ rules[i], src, target ])
- // -- Privileged operation end --
- .then(x => setStatus('Quick Save: SAVED! "' + filename + '" using ' + rules[i].name))
- .catch(err => setStatus('Quick Save: Failed to save "' + filename + ': ' + err + "\n pagetitle: " + pagetitle+ "\n domain: " + domain));
- } else {
- match = []
- }
- }
- setStatus('Quick Save: No matching rule');
- return;
- }());
Add Comment
Please, Sign In to add comment