Safari Extension: change webpage styles or DOM when button is clicked

时间:2016-07-11 20:36:36

标签: safari-extension

I'm making a Safari extension, but am not able to make any visible changes to the webpage using JS injection.

I have added a button in Extension Builder. This is global.html:

safari.application.addEventListener("command", performCommand, false);

function performCommand(event) {
    document.getElementsByTagName("body")[0].style.background = "red";
    alert('no errors!'); // this IS called
}

The alert IS called so the function is executed without errors. However, the body background does not become red. I have tried with different webpages. If I change the body background with Safari's Web Inspector then the page becomes red. The JS by itself is fine: https://jsfiddle.net/f1tdx3za/

In Extension Builder:

Website Access: all (checked 'include secure pages')

Note that the DOM change must be linked to the button click. It should NOT be executed whenever a page loads.

0 个答案:

没有答案