How to make the page script recognize a manually changed value of INPUT element?

前端 未结 1 542
南方客
南方客 2021-01-20 23:47

I\'m working on a Chrome extension and am currently trying to get my content script to automatically do a search using the form on the site. I\'m doing this by changing the valu

相关标签:
1条回答
  • 2021-01-21 00:04

    I can't say without seeing the actual page but it's possible the page checks if the input was "typed", which we can simulate by using document.execCommand:

    document.getElementById('videoSearchInput').focus();
    document.execCommand('selectAll');
    document.execCommand('insertText', false, 'some text');
    

    Or, if you use jQuery, register a new function:

    (function( $ ) {
        $.fn.execInsertText = function(text) {
            var activeElement = document.activeElement;
            var result = this.each(function() {
                this.focus();
                document.execCommand('selectAll');
                document.execCommand('insertText', false, text);
            });
            if (activeElement) {
                activeElement.focus();
            }
            return result;
        };
    }( jQuery ));
    

    then invoke it:

    $("#videoSearchInput").execInsertText('herpderp');
    
    0 讨论(0)
提交回复
热议问题