daasd commited on
Commit
e6da596
·
1 Parent(s): 4c7c1f5

Upload png_auto_download.js

Browse files
Files changed (1) hide show
  1. png_auto_download.js +179 -0
png_auto_download.js ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (()=>{
2
+ let delay_generate_button = function () {
3
+ return new Promise((resolve) => {
4
+ this.shadowRoot = document.querySelector("gradio-app");
5
+ const observer = new MutationObserver((mutations) => {
6
+ mutations.forEach((mutation) => {
7
+ // 如果 id 为 pnginfo_image 的子元素被添加到 gradio-app 元素中
8
+ if (
9
+ mutation.addedNodes.length &&
10
+ mutation.addedNodes[0].id === "txt2img_generate"
11
+ ) {
12
+ // 取消监听
13
+ observer.disconnect();
14
+ // 获取 id 为 pnginfo_image 的元素并进行操作
15
+ resolve();
16
+ }
17
+ });
18
+ });
19
+ observer.observe(this.shadowRoot, { childList: true, subtree: true });
20
+ });
21
+ };
22
+
23
+ //图片下载到本地
24
+ function downloadImage(url, fileName) {
25
+ fetch(url).then(response => {
26
+ return response.blob();
27
+ }).then(blob => {
28
+ const a = document.createElement('a');
29
+ const url = window.URL.createObjectURL(blob);
30
+ a.href = url;
31
+ a.download = fileName;
32
+ a.click();
33
+ window.URL.revokeObjectURL(url);
34
+ });
35
+ }
36
+
37
+ //t2i下载
38
+ let t2idownload=(ParentNode)=>{
39
+ return new Promise((resolve) => {
40
+ const observer = new MutationObserver((mutations) => {
41
+ // console.log('mutations',mutations)
42
+ try{
43
+ mutations.forEach((mutation) => {
44
+ if (mutation.target.localName==='img'||(mutation.addedNodes[0]&&mutation.addedNodes[0].localName==='img'))
45
+ {
46
+ // console.log(mutation);
47
+ t2iState='fufilled';
48
+ [...ParentNode.querySelectorAll('.thumbnail-item > img')].forEach(resNode=>{
49
+ let nameArr=resNode.src.split('/')
50
+ let name=decodeURIComponent(nameArr[nameArr.length-1])
51
+ downloadImage(resNode.src,name)
52
+ })
53
+ observer.disconnect();
54
+ throw '强制跳出循环'
55
+ }});}
56
+ catch(out){
57
+ resolve()
58
+ }
59
+ });
60
+ observer.observe(ParentNode, { attributes: true,childList: true, subtree: true });
61
+ });
62
+ }
63
+
64
+ //i2i下载
65
+ let i2idownload=(ParentNode)=>{
66
+ return new Promise((resolve) => {
67
+ const observer = new MutationObserver((mutations) => {
68
+ // console.log('mutations',mutations)
69
+ try{
70
+ mutations.forEach((mutation) => {
71
+ if (mutation.target.localName==='img'||(mutation.addedNodes[0]&&mutation.addedNodes[0].localName==='img'))
72
+ {
73
+ // console.log(mutation);
74
+ t2iState='fufilled';
75
+ [...ParentNode.querySelectorAll('.thumbnail-item > img')].forEach(resNode=>{
76
+ let nameArr=resNode.src.split('/')
77
+ let name=decodeURIComponent(nameArr[nameArr.length-1])
78
+ downloadImage(resNode.src,name)
79
+ })
80
+ observer.disconnect();
81
+ throw '强制跳出循环'
82
+ }});}
83
+ catch(out){
84
+ resolve()
85
+ }
86
+ });
87
+ observer.observe(ParentNode, { attributes: true,childList: true, subtree: true });
88
+ });
89
+ }
90
+
91
+ //extras下载
92
+ let extdownload=(ParentNode)=>{
93
+ return new Promise((resolve) => {
94
+ const observer = new MutationObserver((mutations) => {
95
+ // console.log('mutations',mutations)
96
+ try{
97
+ mutations.forEach((mutation) => {
98
+ if (mutation.target.localName==='img'||(mutation.addedNodes[0]&&mutation.addedNodes[0].localName==='img'))
99
+ {
100
+ // console.log(mutation);
101
+ extState='fufilled';
102
+ [...ParentNode.querySelectorAll('.thumbnail-item > img')].forEach(resNode=>{
103
+ let nameArr=resNode.src.split('/')
104
+ let name=decodeURIComponent(nameArr[nameArr.length-1])
105
+ downloadImage(resNode.src,name)
106
+ })
107
+ observer.disconnect();
108
+ throw '强制跳出循环'
109
+ }});}
110
+ catch(out){
111
+ resolve()
112
+ }
113
+ });
114
+ observer.observe(ParentNode, { attributes: true,childList: true, subtree: true });
115
+ });
116
+ }
117
+
118
+ async function png_download_init(){
119
+ let self = this;
120
+ await delay_generate_button();
121
+ self.t2iState='pending'
122
+ self.i2iState='pending'
123
+ self.extState='pending'
124
+
125
+ //注册t2i生成按钮点击事件进行下载
126
+ document.querySelector("#txt2img_generate").onclick=async function(){
127
+ //根节点
128
+ let Rootnode=document.querySelector("#txt2img_gallery")
129
+ //第一次获取第一张图片节点,需要从根节点开始找
130
+ if(self.t2iState==='pending')
131
+ {
132
+ await t2idownload.call(self,Rootnode)
133
+ }
134
+ else{
135
+ //对于图片节点的父节点
136
+ let ParentNode=Rootnode.querySelector("#txt2img_gallery > div.grid-wrap > div")
137
+ //下载图片并更新旧节点
138
+ t2idownload.call(self,ParentNode)
139
+ }
140
+ }
141
+
142
+ //注册i2i生成按钮点击事件进行下载
143
+ document.querySelector("#img2img_generate").onclick=async function(){
144
+ //根节点
145
+ let Rootnode=document.querySelector("#img2img_gallery")
146
+ //第一次获取第一张图片节点,需要从根节点开始找
147
+ if(self.i2iState==='pending')
148
+ {
149
+ await i2idownload.call(self,Rootnode)
150
+ }
151
+ else{
152
+ //对于图片节点的父节点
153
+ let ParentNode=Rootnode.querySelector("#img2img_gallery > div.grid-wrap > div")
154
+ //下载图片并更新旧节点
155
+ i2idownload.call(self,ParentNode)
156
+ }
157
+ }
158
+ //注册extra生成按钮点击事件进行下载
159
+ document.querySelector("#extras_generate").onclick=async function(){
160
+ //根节点
161
+ let Rootnode=document.querySelector("#extras_gallery")
162
+ //第一次获取第一张图片节点,需要从根节点开始找
163
+ if(self.extState==='pending')
164
+ {
165
+ await extdownload.call(self,Rootnode)
166
+ }
167
+ else{
168
+ //对于图片节点的父节点
169
+ let ParentNode=Rootnode.querySelector("#extras_gallery > div.grid-wrap > div")
170
+ //下载图片并更新旧节点
171
+ extdownload.call(self,ParentNode)
172
+ }
173
+ }
174
+ }
175
+
176
+ document.addEventListener("DOMContentLoaded", async () => {
177
+ png_download_init()
178
+ });
179
+ })()