Selecting

后端 未结 2 1129
清酒与你
清酒与你 2021-02-10 03:26

I\'m hoplessly trying to activate a change() event from a chrome content script. I\'ve gone past through of the website and google posts, but still nothing works.

here\'

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-10 04:20

    You can append a script. It'll take a lot of script appends but you can always put a custom communication event, and only inject one good script listening to that custom event and eval()ing the event data. It will work if there's no CSP.

    var RunInThisContext = function(c){ try{
        var code = document.createTextNode(c);
        var script = document.createElement('script');
        script.type='text/javascript';
        script.language='javascript';
        script.appendChild(code);
        try{document.body.appendChild(script);}catch(e){document.head.appendChild(script);}
    
    }catch(e){ console.error('ERROR: '+e); }}; 
    

    Use like this:

    RunInThisContext('('+(function(){ 
         $('select').change();
    }).toString()+'()); '); 
    

    But bear in mind you're running this directly into the page. If page doesn't have jQuery you'll have to include it first.

提交回复
热议问题