// ==UserScript==
// @name 保存图片
// @namespace sleasy_su
// @match *://*/*
// @grant GM_download
// @grant GM_registerMenuCommand
// @version 0.3
// @author -
// @description alt+点击保存图片,列出页面所有图片,列出页面所有背景图
// @downloadURL none
// ==/UserScript==
/**
* 0.2
* 1. improve saveImg()
* 0.3
* 1. imporve close button style
*/
init();
function init(){
GM_registerMenuCommand('list bg', listBackgrounds);
GM_registerMenuCommand('list img', listImages);
document.body.addEventListener('click', saveImg);
}
function listBackgrounds(){
var nodes = document.body.querySelectorAll('*');
var backgrounds = [];
var tmp = getBackground(document.body);
tmp != null ? backgrounds.push(tmp) : null;
backgrounds.forEach.call(nodes, v=>{
tmp = getBackground(v);
tmp != null && backgrounds.includes(tmp) != true ? backgrounds.push(tmp) : null;
});
showImages(backgrounds);
function getBackground(node){
var bg = getComputedStyle(node).backgroundImage;
if(bg.includes('url')){
return bg.replace(/.*url\("(.+)"\).*/, '$1');
}else{
return null;
}
}
}
function listImages(){
var images = getSrcs(document.images);
showImages(images);
}
function showImages(imageSet){
var rawHTML = '';
var container = document.createElement('div');
var style = `
#userjs-image-container{width:50%; max-height: 95%; position:fixed; top:20px; box-shadow: 0 0 2px 1px #ddd; background:#fff; margin-left:25%; overflow:auto; z-index:999;}
#userjs-image-list{padding:10px;}
#userjs-image-list>img{max-width:80%; max-height:600px; display:block; padding:10px; margin:10px auto; border: 1px solid #ddd; border-radius:3px;}
button#userjs-btn{position:absolute; top:5px; right:5px; width:2em; height:2em; line-height:2em; border:1px solid #999; background-color:#fff;}`;
imageSet.forEach(v => {
rawHTML += '';
});
container.innerHTML = '