简体   繁体   中英

How to encrypt the formdata message that is sent with the submit action

I have this code that sets attribute "onclick" to all submit buttons to call a certain function. The userscript is going to run on social network sites(Facebook) and encrypt the messages the users send. So I want on the click event to pause the default action, access the message that it sends somehow (I guess with formData ), run encrypt function on the text message and continue the submit action with the message sent encrypted. So here is the script:

$('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
};

It looks like you might be missing a return. In the onclick attribute not having the return before the function will not stop the action from happening.

$('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
};

Instead of placing a onclick function on the submit buttons you can directly define a call the function to collect and encrypt the datas on the form itself as shown below

<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;

}

or otherwise

Simply have a button(input type=button) instead of submit(input type=submit) and define the onclick function as shown below

<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();

}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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