1 year ago
#146399
Nikolai Nagornyi
Why EventListener not fire in page context in Safari App Extension?
I'm trying to do something in the "page context", for this I inject the script in the page like this
var pageScript = document.createElement('script');
pageScript.textContent = actualCode;
document.body.insertBefore(pageScript, document.body.firstChild);
the actualCoe
is
var actualCode = '(' + function() {
document.addEventListener(playerEventName, function(event) {
console.log("page script fire"");
});
} + ')();'; // end actualCode
but listener doesn't not fire when i sent message, in console i see only sent message name.
The full code of my script.js file
document.addEventListener("DOMContentLoaded", function(event) {
safari.extension.dispatchMessage("script injected successfully");
init();
});
let playerEventName = "su.nagornyi.yam.ext.evnt";
var init = function() {
console.log("init started");
var pageScript = document.createElement('script');
pageScript.textContent = actualCode;
document.body.insertBefore(pageScript, document.body.firstChild);
}; // end init
var actualCode = '(' + function() {
document.addEventListener(playerEventName, function(event) {
console.log("page script fire");
});
} + ')();'; // end actualCode
safari.self.addEventListener("message", handleMessage);
function handleMessage(event) {
console.log(event.name);
let playerEvent = new CustomEvent(playerEventName, {
detail: {
action: event.name
}
});
document.dispatchEvent(playerEvent);
}; // end handleMessage
UPD: Error in console:
Refused to execute a script because its hash its nonce or 'unsafe-inline'does
not appear in the script-src directive of the Content Securrity policy.
So, is it possible to inject scrip on this page?
UPD2: In Safari App Extension we can inject scripts only from file placed in extension bundle.
javascript
addeventlistener
event-listener
custom-events
safari-app-extension
0 Answers
Your Answer