26 lines
578 B
JavaScript
26 lines
578 B
JavaScript
function downloadResource(info, tab) {
|
|
let url = info['srcUrl']
|
|
|
|
let filename = url.substring(url.lastIndexOf('/')+1)
|
|
if (filename.indexOf('?') !== false)
|
|
filename = filename.substring(0, filename.indexOf('?'))
|
|
filename = safeFileName(filename)
|
|
|
|
chrome.downloads.download({
|
|
url,
|
|
filename,
|
|
saveAs: false
|
|
})
|
|
}
|
|
|
|
chrome.contextMenus.create({
|
|
"title": "Save to Downloads…",
|
|
"contexts": ["image"],
|
|
"onclick": downloadResource
|
|
})
|
|
|
|
function safeFileName(str) {
|
|
if (/[|"*?:<>]/.test(str))
|
|
str = str.replace(/[\/\\|"*?:<>]/g, '_');
|
|
return str
|
|
} |