-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
190 lines (175 loc) · 5.63 KB
/
content.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
console.log("Chrome Extension(trial) executes!!");
// Anonymous function (Just to teach myself a little more about them, not necessary to use)
(function () {
$(window).on("load", _onInputFileClickFnBind);
})();
// --------------------------------------- Below code is to observe changes(mutation) in dom and webNaviagtion --------------------------------------- //
// Select the node that will be observed for mutations
const targetNode = document.body;
// Options for the observer (which mutations to observe)
const config = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = function (mutationsList, observer) {
// Use traditional 'for loops' for IE 11
try {
for (const mutation of mutationsList) {
// console.log("mutation.addedNodes", mutation.addedNodes);
if (mutation && mutation.addedNodes && mutation.addedNodes.length) {
for (let i = 0; i < mutation.addedNodes.length; i++) {
if (
mutation.addedNodes[i] instanceof HTMLElement &&
mutation.addedNodes[i].querySelectorAll("input[type=file]").length >
0
) {
_onInputFileClickFnBind();
}
}
}
}
} catch (err) {
console.log(err);
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
// observer.disconnect();
// --------------------------------------- Exit --------------------------------------- //
var current_file_input;
var iframe = document.createElement("iframe");
const get_modal_url = chrome.extension.getURL("modal.html");
iframe.src = get_modal_url;
iframe.style.cssText =
"position: fixed; border: none; overflow: scroll; left: 0px; top: 0px; height: 100vh; width: 100vw; z-index: 20000000; display: grid; place-items: center;";
iframe.id = "fileDialogFrame";
function _onInputFileClickFnBind() {
$("input[type=file]").each(function () {
_onInputClick($(this));
});
}
function _onInputClick(input) {
input.off("click");
input.on("click", async (event) => {
event.preventDefault();
current_file_input = input;
// $(iframe).appendTo("body");
document.body.appendChild(iframe);
});
}
window.addEventListener(
"message",
async (event) => {
switch (`${event.data.type}`) {
case "iframeLoaded":
try {
// One way to send data back to the child iframe
if (
iframe &&
iframe.src &&
event.origin === iframe.src.split("/").splice(0, 3).join("/")
) {
var _window = iframe.contentWindow;
_window.postMessage(
{
multiple: current_file_input.attr("multiple") ? true : false,
},
iframe.src
);
}
} catch (err) {
console.log(err);
}
break;
case "hideFrame":
$("#fileDialogFrame").remove();
break;
case "openLocalFileDialog":
try {
current_file_input.off("click");
current_file_input.click();
_onInputClick(current_file_input);
} catch (err) {
console.log(err);
}
break;
case "uploadFiles":
try {
// Second way to send data back to the child iframe
_uploadFiles(event.data.src_arr)
.then((res) => {
event.source.postMessage(
{ type: "uploadResponse" },
event.origin
);
})
.catch((err) => {
console.log(err);
try {
event.source.postMessage(
{ type: "uploadError", error: err },
event.origin
);
} catch (error) {
console.log(error);
} finally {
alert(err);
}
});
} catch (err) {
console.log(err);
}
break;
default:
$("#fileDialogFrame").remove();
}
},
false
);
async function _uploadFiles(file_src_arr) {
let list = new DataTransfer();
const URLtoFile = (url) =>
fetch(url, {
mode:
"cors" /* OPTIONS >>>>>>>> same-origin, no-cors, cors(given by default) */,
})
.then((response) => response.blob())
.then((blob) => {
const file_extension = blob.type.split("/")[1];
const filename_index = url.lastIndexOf("/") + 1;
const filename = `${url
.substr(filename_index)
.split("?")[0]
.replace(`.${file_extension}`, "")}`.replace(".", "_");
let file_obj = new File([blob], `${filename}.${file_extension}`, {
type: blob.type,
});
return Promise.resolve(file_obj);
})
.catch((err) => {
console.log(err);
return Promise.reject(new Error(err.message));
});
await Promise.all(
file_src_arr.map(async (file_src) => {
await URLtoFile(file_src)
.then((fileData) => {
list.items.add(fileData);
})
.catch((err) => {
console.log(err);
throw JSON.stringify(err.message);
});
})
);
const customisedFileList = list.files;
if (customisedFileList.length) {
current_file_input.prop("files", customisedFileList);
}
const onchange_event = new Event("change", { bubbles: true });
current_file_input.get(0).dispatchEvent(onchange_event);
console.log("File/s Uploaded successfully!!");
return JSON.stringify("File/s Uploaded successfully!!");
}