onclick or inline script isn't working in extension

前端 未结 5 1102
渐次进展
渐次进展 2020-11-21 04:57

This seems to be the easiest thing to do, but it\'s just not working. In a normal browser the .html and .js files works perfectly, but in the Chrome/Firefox extension the

5条回答
  •  甜味超标
    2020-11-21 05:26

    As already mentioned, Chrome Extensions don't allow to have inline JavaScript due to security reasons so you can try this workaround as well.

    HTML file

    
        
            
                
                    Getting Started Extension's Popup
                
                
            
            
                
    ha

    hyhy

    popup.js

    window.onclick = function(event) {
        var target = event.target ;
        if(target.matches('.clickableBtn')) {
            var clickedEle = document.activeElement.id ;
            var ele = document.getElementById(clickedEle);
            alert(ele.text);
        }
    }
    

    Or if you are having a Jquery file included then

    window.onclick = function(event) {
        var target = event.target ;
        if(target.matches('.clickableBtn')) {
            alert($(target).text());
        }
    }
    

提交回复
热议问题