Open tab with post data to that tab in chrome extensions

后端 未结 1 1095
野趣味
野趣味 2021-01-13 23:01

I would like to open a new tab with posting some data to that tab. How can I do this ?

Thank in advance for your help.

1条回答
  •  臣服心动
    2021-01-13 23:31

    You could create a form, and submit it via JavaScript. A general example can be found here. This can be done from anywhere within your extension, ranging from the background page to content scripts.

    If you need more control over the location of the form without content scripts, you can use data-URLs, with the following generic format:

    data:text/html;charset=utf8,

    Or, programatically:

    var url = 'data:text/html;charset=utf8,';
    function append(key, value) {
        var input = document.createElement('textarea');
        input.setAttribute('name', key);
        input.textContent = value;
        form.appendChild(input);
    }
    var form = document.createElement('form');
    form.method = 'POST';
    form.action = 'http://rob.lekensteyn.nl/dump.php';
    append('q', 'query');
    url += encodeURIComponent(form.outerHTML);
    url += encodeURIComponent('');
    // A general method
    window.open(url);
    // Or, use chrome extension-specific method which offers more control
    // chrome.tabs.create({url: url, active: true});
    

    PS. Source code of http://rob.lekensteyn.nl/dump.php (used to show headers and POST data):

     $value) {
        if (strpos($header, 'HTTP_') === 0) {
            echo str_pad(substr($header, 5), 20) . " " . $value . "\n";
        }
    }
    echo "\n\n";
    $input = fopen('php://input', 'rb');
    while (!feof($input)) {
        echo fread($input, 102400);
    }
    fclose($input);
    ?>
    

    0 讨论(0)
提交回复
热议问题