简体   繁体   English

Wordpress上传插件表单无法在无头浏览器中激活

[英]Wordpress Upload Plugin Form Will Not Activate In Headless Browser

I am working on a little project where PhamtomJS will log into my site and upload a plugin to it. 我正在开发一个小项目,PhamtomJS将登录我的网站并上传一个插件。 I almost have it, except for one strange problem. 我几乎拥有它,除了一个奇怪的问题。 Although the file.upload portion of my code works perfectly, the Install now button remains disabled. 虽然我的代码的file.upload部分工作正常,但“立即安装”按钮仍然处于禁用状态。 Here is a picture rendered from Phantom: 这是Phantom渲染的图片:

截图

As you can see, the button is still disabled. 如您所见,该按钮仍处于禁用状态。 So when I try to click it using the getElementById() method, it will not work. 因此,当我尝试使用getElementById()方法单击它时,它将无法工作。 Nor will it work if I try to submit the form by doing: 如果我尝试通过以下方式提交表单,它也不会起作用:

document.querySelector('form[action*="/wp-admin/update.php?action=upload-plugin"]').submit()

I have also tried enabling the button manually and then clicking it via Javascript, but that has not worked either. 我也尝试手动启用按钮,然后通过Javascript单击它,但这也没有用。 Any ideas? 有任何想法吗?

EDIT When trying to pull the outer HTML of the button, I typed the following into console : 编辑当试图拉出按钮的外部HTML时,我在console输入以下内容:

console.log(document.querySelector('#install-plugin-submit').outerHTML)

The result of this code was the following: 此代码的结果如下:

<input type="submit" name="install-plugin-submit" id="install-plugin-submit" class="button" value="Install Now">

How did you get the value into the file input box? 你是如何将值输入文件输入框的? Depending on the browser (client) implementation, simply adding text as the file input value likely will not trigger the box's change event. 根据浏览器(客户端)的实现,只需添加文本作为文件输入值可能不会触发框的change事件。

The first (read: simplest) solution may be to try $('form.wp-upload-form input[type="file"]').trigger('change'); 第一个(读取:最简单)解决方案可能是尝试$('form.wp-upload-form input[type="file"]').trigger('change'); after you whack in the value. 在你重视价值之后。 (If you're not using jQuery, see here on how to trigger a native event; it looks like you already know how to select nodes. PhantomJS may very well have a native trigger function because it's the kind of thing you do in that kind of application.) (如果您没有使用jQuery,请参阅此处有关如何触发本机事件;看起来您已经知道如何选择节点.PhantomJS可能很好地具有本机触发器功能,因为它是您在那种类型中执行的操作申请。)

Failing that, you may want to simply upload the file through JS. 如果做不到这一点,您可能只想通过JS上传文件。 I'm on 4.7.3 and my form looks like this: 我在4.7.3上,我的表单看起来像这样:

<form method="post" enctype="multipart/form-data" class="wp-upload-form" action="http://[server]/wp-admin/update.php?action=upload-plugin"> <input type="hidden" id="_wpnonce" name="_wpnonce" value="[nonce]"><input type="hidden" name="_wp_http_referer" value="/wp-admin/plugin-install.php"> <label class="screen-reader-text" for="pluginzip">Plugin zip file</label> <input type="file" id="pluginzip" name="pluginzip"> <input type="submit" name="install-plugin-submit" id="install-plugin-submit" class="button" value="Install Now" disabled=""> </form>

Put together a FormData , scrape the submit URL from the action tag on the form, make sure you include the nonce and probably pass the submit value for good measure. FormData放在一起,从表单上的action标签中抓取提交URL,确保包含nonce并可能传递提交值以获得良好的度量。 Assembling a multipart/form-data POST is annoying without a suitable plugin but if you've gotten this far you've probably had to deal with more annoying stuff already. 在没有合适的插件的情况下组装multipart/form-data POST很烦人但是如果你已经到了这么远,你可能已经不得不处理更烦人的东西了。

WordPress shouldn't be able to tell the difference between what you submit from its own form or what you fake together and throw at the same URL. WordPress 不能区分你从自己的表单提交的内容或你假装在一起并扔在同一个URL上的内容。 If it is, check the client string setting, make sure you've got the nonce right and make sure you're setting the referer [sic.] correctly. 如果是,请检查客户端字符串设置,确保您已将nonce设置为正确并确保正确设置referer [sic。]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM