-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
122 lines (97 loc) · 5.34 KB
/
worker.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const baseuri = "https://gelbooru.com"
function parseURIParameters(uri){
if(typeof uri !== "string") return null
const splt = "<s>"
const pstr = uri.split("?").join(splt).split("&").join(splt).split(splt).slice(1)
const objParams = {}
pstr.forEach(ps => {
const k = ps.split("=")
objParams[k[0]] = decodeURIComponent(k[1])
})
return objParams
}
function isNNN(){
const curDate = new Date()
return (new Date(`1 November ${curDate.getFullYear()}`) < curDate) && (new Date(`31 November ${curDate.getFullYear()}`) > curDate)
}
chrome.runtime.onInstalled.addListener(details => {
if(details.reason === "install"){
chrome.storage.local.set({ nnnblock: true })
}
if(isNNN()){
chrome.storage.local.get(['nnnblock'], options => {
if(options.nnnblock){
chrome.tabs.query({}).then(tabs => tabs.filter(tab => tab.url.startsWith(baseuri)).forEach(tab => chrome.tabs.remove(tab.id))).catch(err => undefined)
}
})
}
})
chrome.tabs.onUpdated.addListener((tabId, tab) => {
if(isNNN()){
if(tab.status !== "loading") return
if(tab.url){
if(!tab.url.startsWith(baseuri)) return
} else {
return
}
chrome.storage.local.get(['nnnblock'], options => {
if(options.nnnblock){
chrome.tabs.query({}).then(tabs => tabs.filter(tab => tab.url.startsWith(baseuri)).forEach(tab => chrome.tabs.remove(tab.id))).catch(err => undefined)
}
})
}
})
chrome.runtime.onConnect.addListener(port => {
port.onMessage.addListener(message => {
let setInfobox = (content, textColor) => port.postMessage({ type: "action", action: "setInfobox", data: { content: (typeof content === "string" ? content : "noContent"), textColor: (typeof textColor === "string" ? textColor : "greenyellow") } })
let throwError = (err) => port.postMessage({ type: "action", action: "setInfobox", data: { content: (err ? err.toString() : "noErr"), textColor: "red" } })
let throwWarning = (warning) => port.postMessage({ type: "action", action: "setInfobox", data: { content: `/!\\ ` + (typeof warning === "string" ? warning : "noWarning"), textColor: "yellow" } })
chrome.tabs.query({}).then(tabs => {
var posttabs = tabs.filter(tab => {
const up = parseURIParameters(tab.url)
if(tab.url.startsWith(baseuri) && (up.page === "post") && (up.s === "view") && (!isNaN(up.id))){
return true
} else {
return false
}
})
if(message.type === "autoclosetabs_swchange"){
chrome.storage.local.set({ autoclosetabs: message.data })
} else if(message.type === "nnnblock_swchange"){
chrome.storage.local.set({ nnnblock: !message.data })
if(isNNN() && !message.data){
chrome.tabs.query({}).then(tabs => tabs.filter(tab => tab.url.startsWith(baseuri)).forEach(tab => chrome.tabs.remove(tab.id))).catch(throwError)
}
} else {
chrome.storage.local.get(null, options => {
if(options.nnnblock && isNNN()){
throwError("Nuh-uh :/")
tabs.filter(tab => tab.url.startsWith(baseuri)).forEach(tab => chrome.tabs.remove(tab.id))
return
}
if(!posttabs.length) return throwWarning("No Gelbooru post tabs found.")
if(message.type === "cpgl"){
port.postMessage({ type: "action", action: "clipboardWriteText", data: { content: posttabs.map(tab => tab.url).join("\n") } })
setInfobox("Gelbooru post links copied to clipboard ;>>")
} else if(message.type === "dgpi"){
const fposttabs = [...new Set(posttabs.map(tab => parseURIParameters(tab.url).id))]
fposttabs.forEach(id => {
fetch(`${baseuri}/index.php?page=dapi&s=post&q=index&id=${id}&json=1`, { method: "GET" }).then(res => res.json()).then(res => {
const image_uri = res.post[0].file_url
const image_ext = image_uri.substring(image_uri.lastIndexOf('/') + 1).split(".")
chrome.downloads.download({ url: image_uri, filename: `gelbooru_${res.post[0].id}${image_ext[1] ? `.${image_ext[1]}` : ""}` }).catch(throwError)
}).catch(throwError)
})
setInfobox(`Downloading ${posttabs.length} post images...`)
} else if(message.type === "closetabs"){
posttabs.forEach(tab => chrome.tabs.remove(tab.id))
setInfobox("Closed all the post tabs.")
}
if(message.type !== "closetabs" && options.autoclosetabs){
posttabs.forEach(tab => chrome.tabs.remove(tab.id))
}
})
}
}).catch(throwError)
})
})