Javascript 如何加密随提交操作发送的formdata消息
我有一段代码,它将属性“onclick”设置为所有提交按钮以调用某个函数。 用户脚本将在社交网站(Facebook)上运行,并对用户发送的消息进行加密。 因此,我希望在单击事件上暂停默认操作,访问它以某种方式发送的消息(我猜是使用formData),对文本消息运行加密功能,并在发送加密消息的情况下继续提交操作。 下面是脚本:Javascript 如何加密随提交操作发送的formdata消息,javascript,forms,greasemonkey,jquery-events,userscripts,Javascript,Forms,Greasemonkey,Jquery Events,Userscripts,我有一段代码,它将属性“onclick”设置为所有提交按钮以调用某个函数。 用户脚本将在社交网站(Facebook)上运行,并对用户发送的消息进行加密。 因此,我希望在单击事件上暂停默认操作,访问它以某种方式发送的消息(我猜是使用formData),对文本消息运行加密功能,并在发送加密消息的情况下继续提交操作。 下面是脚本: $('textarea', window.content.document) .closest('form') .find('input[type=submit]') .a
$('textarea', window.content.document)
.closest('form')
.find('input[type=submit]')
.attr("onclick","dont();");
function dont(){
//access formData sent with the submit action and encrypt the message
};
看起来你可能错过了一次返程。在onclick属性中,在函数之前没有返回将不会停止操作的发生
$('textarea', window.content.document)
.closest('form')
.find('input[type=submit]')
.attr("onclick","return dont();");
function dont(){
//access formData sent with the submit action and encrypt the message
//be sure to return true or false depending on if you want the action submitted
};
不必在提交按钮上放置onclick函数,您可以直接定义调用该函数来收集和加密表单本身上的数据,如下所示
<form name="sampleForm" method="post" onsubmit="return dont()" enctype="multipart/form-data" action="someurl">
function dont()
{
//code to access the form data and encrypt
if(error)
return false; //Stops the submission of the form
else
return true;
}
<input type="button" value="submit" onclick="dont()" />
function dont()
{
//code to access the form data and encrypt
if(error)
alert("Failed") //Stops the submission of the form
else
document.forms[0].submit();
}
函数dont()
{
//用于访问表单数据和加密的代码
如果(错误)
return false;//停止表单的提交
其他的
返回true;
}
否则
只需使用一个按钮(输入类型=按钮)而不是submit(输入类型=submit),并定义如下所示的onclick函数
<form name="sampleForm" method="post" onsubmit="return dont()" enctype="multipart/form-data" action="someurl">
function dont()
{
//code to access the form data and encrypt
if(error)
return false; //Stops the submission of the form
else
return true;
}
<input type="button" value="submit" onclick="dont()" />
function dont()
{
//code to access the form data and encrypt
if(error)
alert("Failed") //Stops the submission of the form
else
document.forms[0].submit();
}
函数dont()
{
//用于访问表单数据和加密的代码
如果(错误)
警报(“失败”)//停止表单的提交
其他的
document.forms[0]。提交();
}
希望这能有所帮助。您到底想通过加密数据来阻止谁访问数据?作为一种补充,您也可以编写
,而不是使用attr
来设置单击处理程序。单击(不)
取而代之。@32bitkid我正试图保护facebook和谷歌等大公司的数据安全,让政府更难或不可能搜索用户编写的政治上“不正确”的关键词。