Can't find variable: safari ?

According to the doc from apple(https://developer.apple.com/documentation/safariservices/safari_app_extensions/injecting_a_script_into_a_webpage?changes=l_3&language=objc), the js thow exception as follows:

function run() {
    var newElement = document.createElement("img");
    newElement.src = safari.extension.baseURI + "bb.png";
    document.body.insertBefore(newElement,
    document.body.firstChild);
}

if(document.readyState !== 'loading') {
    run();
} else {
    document.addEventListener("DOMContentLoaded", function(e){
        run();
    });
}

the error message is : Can't find variable: safari what should I do?

Has anyone found a solution to this? I am getting the same "Can't find variable: safari" console error. If I refresh the page it goes away. But on the initial load it is always there.

Can't find variable: safari ?
 
 
Q