-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.js
148 lines (146 loc) · 6.04 KB
/
content_script.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
document.addEventListener("dblclick", inject_box);
document.addEventListener("click", ClickEvent);
function ClickEvent() {
if (document.getElementById("hbox_main") != null){
fadeOut(document.getElementById("hbox_main"));
}
try{
chrome.runtime.sendMessage({
action: "createContext",
value: getSelectionText()
}, function(response) {
});
}catch(exc){}
}
function inject_box() {
let text = getSelectionText();
text = text.replace(" " , "");
let result;
let result_raw;
if (!isNaN(text) && text.length > 1 && text.length < 8) {
try{
chrome.storage.sync.get(['h_backend', 'h_min', 'h_max', 'h_showcover', 'h_tags', 'h_popup'], function(items) {
if (items.h_popup && Number(text) >= items.h_min && Number(text) <= items.h_max) {
let backend = items.h_backend;
var request = new XMLHttpRequest();
var url = backend === 'nhentai' ? 'https://nhentai.net/api/gallery/' + text : 'https://h.api.rayriffy.com/v1/gallery/' + text;
request.open("GET", url, true);
request.onload = function() {
if (backend == 'nhentai') {
result = JSON.parse(this.response);
}else {
result = JSON.parse(this.response);
result = result.response.data;
}
result_raw = this.response;
if (request.status >= 200 && request.status < 400) {
let selection = window.getSelection().getRangeAt(0);
let rect = selection.getBoundingClientRect();
let ex = document.createElement("div");
if (result_raw.includes('media_id') === false) {
ex.innerHTML = '<div class="hbox_title">No gallery found.</div>';
} else {
let tags_map = new Map();
for (t in result.tags) {
if (tags_map.has(result.tags[t].type)) {
tags_map.set(
result.tags[t].type,
tags_map.get(result.tags[t].type) +
", <font class=\"clicker\" onClick=\"window.open('https://nhentai.net/"+result.tags[t].type+"/"+replaceAll(result.tags[t].name," ", "-")+"', '_blank');\">" + capitalizeFLetter(result.tags[t].name) + "</font>"
);
} else {
tags_map.set(
result.tags[t].type,
"<font class=\"clicker\" onClick=\"window.open('https://nhentai.net/"+result.tags[t].type+"/"+replaceAll(result.tags[t].name," ", "-")+"', '_blank');\">" + capitalizeFLetter(result.tags[t].name) + "</font>"
);
}
}
let tags = "";
for (let [key, value] of tags_map) {
tags += '<p class="hbox_tags"><b>' + capitalizeFLetter(key) + ":</b> " + capitalizeFLetter(value) + "</p>";
}
let image_src =
"https://t.nhentai.net/galleries/" +
result.media_id +
"/cover." +
(result.images.cover.t === "p" ? "png" : "jpg");
ex.innerHTML = "<meta name='referrer' content='same-origin' />"+ (items.h_showcover ? '<a target="_blank" href="https://nhentai.net/g/'+ text +'"> <img class="hbox_book_cover" id="hbox_book_cover" onerror="this.onerror=null; document.getElementById(\'hbox_book_cover\').remove();" src="' +
image_src +
'"></img></a>' : '') +
'<a class="clicker" target="_blank" href="https://nhentai.net/g/'+ text +'"><div class="hbox_title">' +
result.title.japanese +
'</div> <div class="hbox_title">' +
result.title.english +
"</div></a> " + (items.h_tags ? tags + ' <div class="hbox_text_right hbox_tags">[' + (backend === 'nhentai' ? result.num_pages : result.images.pages.length) : '') + ' Pages]</div>' ;
}
ex.setAttribute("id", "hbox_main");
ex.setAttribute(
"style",
`top: ${rect.y + window.pageYOffset + 25}px; left: ${rect.x +
window.pageXOffset -
120}px;`
);
document.body.appendChild(ex);
hide(ex);
setTimeout(function(){
fadeIn(ex);
}, 50);
} else {
let selection = window.getSelection().getRangeAt(0);
let rect = selection.getBoundingClientRect();
let ex = document.createElement("div");
ex.innerHTML = '<div class="hbox_title">No gallery found.</div>';
ex.setAttribute("id", "hbox_main");
ex.setAttribute(
"style",
`top: ${rect.y + window.pageYOffset + 25}px; left: ${rect.x +
window.pageXOffset -
120}px;`
);
document.body.appendChild(ex);
hide(ex);
setTimeout(function(){
fadeIn(ex);
}, 50);
}
};
request.send();
}
});
}catch(exr){}
}
}
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function fadeIn(el){
if (document.getElementById("hbox_main") != null){}
el.classList.add('show');
el.classList.remove('hide');
el.classList.remove('hide_fast');
}
function fadeOut(el){
el.classList.add('hide');
el.classList.remove('show');
el.classList.remove('hide_fast');
setTimeout(function(){
document.getElementById("hbox_main").remove();
}, 300);
}
function hide(el){
el.classList.remove('hide');
el.classList.remove('show');
el.classList.add('hide_fast');
}
function capitalizeFLetter(text) {
return text.replace(/^./, text[0].toUpperCase());
}
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}