Chrome 扩展开发 - POST 到新标签 [英] Chrome Extension Development - POST to new tab

查看:31
本文介绍了Chrome 扩展开发 - POST 到新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的解决方案可以将动态聚合的数据发布到新选项卡中?

Is there an easy solution to POST dynamically aggregated data into a new tab?

chrome.tabs.create 没有POST"选项.通常我会使用

chrome.tabs.create does not have a 'POST' option. Normally I would use

chrome.browserAction.onClicked.addListener(function (t) {
  chrome.tabs.create(
  {
    "url" : "http://super.url",
    "method" : "POST" // oops.. no option.
  });
});

推荐答案

你可以简单地结合这两种技术:

You can simply combine these two techniques:

  1. 您可以通过在地址栏或 标签的 href 值中添加 javascript: 前缀来执行 JavaScript 命令.
  2. 只有使用 JavaScript,您才能创建表单元素并用您的数据填充它,然后发布它.
  1. You may execute JavaScript commands by adding javascript: prefix at your address bar or in href values of <a> tags.
  2. Only with JavaScript, you can create a form element and fill it with your data then POST it.

function fakePost() {
    var form = document.createElement("form");
    
    // Create a POST dump at ptsv2.com and change the code
    form.setAttribute("action", "http://ptsv2.com/t/dcgex-1614815819/post");
    form.setAttribute("method", "post");

    var params = { userId: 2, action: "delete" };
    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);
        form.appendChild(hiddenField);
    }

    // Appending the form might not be necessary
    document.body.appendChild(form);

    form.submit();
};

const 
    source = fakePost.toString().replace(/(
|	)/gm,'').replace(/ss/gm,' '),
    url = `javascript:${source}; fakePost();`;

chrome.browserAction.onClicked.addListener(() => chrome.tabs.create({ url }));

当然,这只是一个肮脏的黑客.如果您需要更详细的内容,可以使用 XHR 对象或@Xan 的回答.

Of course, that's just a dirty hack. If you need something more elaborate you can use a XHR Object or @Xan's answer.

这篇关于Chrome 扩展开发 - POST 到新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆