-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
56 lines (48 loc) · 1.42 KB
/
content.js
File metadata and controls
56 lines (48 loc) · 1.42 KB
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
function isJSONContentType() {
const contentType = document.contentType || '';
return contentType.toLowerCase().includes('application/json');
}
function isValidJSON(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
function getExtensionURL(path) {
return chrome.runtime.getURL(path);
}
async function injectViewer(jsonContent) {
// Create iframe for the viewer
const iframe = document.createElement('iframe');
iframe.style.cssText = 'position: fixed; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: 100%; border: none; background: white; z-index: 2147483647;';
iframe.src = getExtensionURL('index.html');
// Add iframe to page
document.documentElement.innerHTML = '';
document.body.appendChild(iframe);
// Wait for viewer to be ready
window.addEventListener('message', function(event) {
// Only accept messages from our viewer
if (event.data === 'viewer-ready') {
// Send JSON data to viewer
iframe.contentWindow.postMessage({
type: 'json-data',
content: jsonContent
}, '*');
}
});
}
function processJSON() {
const bodyText = document.body.textContent;
if (isValidJSON(bodyText)) {
// If valid JSON is found, inject the viewer
injectViewer(bodyText);
}
}
// Run on page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', processJSON);
} else {
processJSON();
}