onclick or inline script isn't working in extension

前端 未结 5 1103
渐次进展
渐次进展 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:37

    I had the same problem, and didn´t want to rewrite the code, so I wrote a function to modify the code and create the inline declarated events:

    function compile(qSel){
        var matches = [];
        var match = null;
        var c = 0;
    
        var html = $(qSel).html();
        var pattern = /(<(.*?)on([a-zA-Z]+)\s*=\s*('|")(.*)('|")(.*?))(>)/mg;
    
        while (match = pattern.exec(html)) {
            var arr = [];
            for (i in match) {
                if (!isNaN(i)) {
                    arr.push(match[i]);
                }
            }
            matches.push(arr);
        }
        var items_with_events = [];
        var compiledHtml = html;
    
        for ( var i in matches ){
            var item_with_event = {
                custom_id : "my_app_identifier_"+i,
                code : matches[i][5],
                on : matches[i][3],
            };
            items_with_events.push(item_with_event);
            compiledHtml = compiledHtml.replace(/(<(.*?)on([a-zA-Z]+)\s*=\s*('|")(.*)('|")(.*?))(>)/m, "<$2 custom_id='"+item_with_event.custom_id+"' $7 $8");
        }
    
        $(qSel).html(compiledHtml);
    
        for ( var i in items_with_events ){
            $("[custom_id='"+items_with_events[i].custom_id+"']").bind(items_with_events[i].on, function(){
                eval(items_with_events[i].code);
            });
        }
    }
    
    $(document).ready(function(){
        compile('#content');
    })
    

    This should remove all inline events from the selected node, and recreate them with jquery instead.

提交回复
热议问题