-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
54 lines (48 loc) · 1.38 KB
/
content-script.js
File metadata and controls
54 lines (48 loc) · 1.38 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
// Canvas Editor DevTools - Content Script
// Inject into page, bridge between DevTools and Editor instance
(function () {
'use strict'
// Inject script into page
function injectScript(src) {
const script = document.createElement('script')
script.src = chrome.runtime.getURL(src)
script.onload = function () {
this.remove()
}
script.onerror = function () {
this.remove()
}
const target = document.head || document.documentElement
if (target) {
target.appendChild(script)
}
}
// Listen for messages from page, forward to DevTools
window.addEventListener('message', function (event) {
if (event.source !== window) return
if (!event.data || event.data.source !== 'canvas-editor-devtools-page')
return
// Check if extension context is valid
if (!chrome.runtime || !chrome.runtime.sendMessage) {
return
}
try {
// Forward to background - directly forward type and payload
chrome.runtime.sendMessage({
type: event.data.type,
payload: event.data.payload
})
} catch (e) {
// ignore
}
})
// Inject script after DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function () {
injectScript('injected-script.js')
})
} else {
// DOM is already ready
injectScript('injected-script.js')
}
})()