我需要在后台发布Greasemonkey。我尝试动态创建iframe并发布到它,但它不起作用:
I need to post in the background with Greasemonkey. I tried to create an iframe dynamically and post to it, but it didn't work:
function crossDomainPost() { // Add the iframe with a unique name var iframe = document.createElement("iframe"); var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING"; document.body.appendChild(iframe); iframe.style.display = "none"; iframe.contentWindow.name = uniqueString; // construct a form with hidden inputs, targeting the iframe var form = document.createElement("form"); form.target = uniqueString; form.action = "INSERT_YOUR_URL_HERE"; form.method = "POST"; // repeat for each parameter var input = document.createElement("input"); input.type = "hidden"; input.name = "INSERT_YOUR_PARAMETER_NAME_HERE"; input.value = "INSERT_YOUR_PARAMETER_VALUE_HERE"; form.appendChild(input); document.body.appendChild(form); form.submit(); }有人说即使我们发布,我们也是将无法接受这些价值观。如果我们不能,只需让用户访问该页面即可。它可以在JS,jQuery,AJAX帖子中。不仅仅是form-iframe技巧。
Some people say even if we post, we won't be able take the values. if we can't, just making the user visit the page is enough. it can be in JS, jQuery, AJAX post. Not only the form-iframe trick.
推荐答案Greasemonkey内置了对跨域发布的支持。您不需要使用jsonp,也不需要使用iframe。使用 GM_xmlhttpRequest函数。
Greasemonkey has built-in support for cross-domain posting. You do not need to use jsonp, nor an iframe. Use the GM_xmlhttpRequest function.
相反而不是尝试构建表单并发布它,您直接发送表单编码数据:
Rather than trying to build a form and post it, you send form-encoded data directly:
var formData1 = "1 INSERT_YOUR_PARAMETER_VALUE_HERE"; var formData2 = "2 INSERT_YOUR_PARAMETER_VALUE_HERE"; var formData3 = "3 INSERT_YOUR_PARAMETER_VALUE_HERE"; // etc. GM_xmlhttpRequest ( { method: "POST", url: "YOUR_SERVER.COM/YOUR_PATH", data: "formData1=" + encodeURIComponent (formData1) + "&" + "formData2=" + encodeURIComponent (formData2) + "&" + "formData3=" + encodeURIComponent (formData3) // etc. , headers: { "Content-Type": "application/x-www-form-urlencoded" }, onload: function (response) { console.log (response.responseText); } } );更多推荐
使用Greasemonkey进行跨域帖子?
发布评论