小编toa*_*ext的帖子

如何检查解密是否正确?

我正在聊天室为多个用户加密消息,每个用户可能有不同的加密和密钥/密码.因此,用户的密钥不适用于所有消息; 返回错误.

var message ="secret message";
var encrypted = CryptoJS.AES.encrypt(message, "Secret Passphrase");
try {
    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase123").toString(CryptoJS.enc.Utf8);
    if (decrypted.length > 0) {
        alert(decrypted);
    } else {
        alert("false");
    }
} catch(e) {
    alert("false");
}
Run Code Online (Sandbox Code Playgroud)

我目前正在捕捉错误,但有时解密返回混乱的字母和符号.我目前这样做的方式效率不高.有没有更好的办法?

javascript encryption cryptojs

4
推荐指数
1
解决办法
1129
查看次数

从 URL 获取文件类型

我需要从服务器上的 url 图像中找出文件类型而不检查扩展名,但我不知道如何在不将图像放入“输入”的情况下执行此操作,如下所示:

 <input type="file" id="upload_file" accept="image/*|audio/*|video/*"/>
 <input type="submit"  onclick="sumbit()"/>

<script type="text/javascript">
    function sumbit(){
        var file_Element = document.getElementById("upload_file")
        alert(file_Element.files[0].type);
        //alert: image/png
    } 
<script>
Run Code Online (Sandbox Code Playgroud)

我知道“.type”仅适用于文件对象,那么如何将 url 图像转换为像 google 徽标图像这样的对象: https: //www.google.ca/images/branding/googlelogo/1x/ googlelogo_color_272x92dp.png。我需要使用 ajax/flilereader 吗?如果是这样,怎么办?

javascript

2
推荐指数
1
解决办法
7361
查看次数

用户切换选项卡时如何停止Javascript功能或最小化浏览器

我正在创建一个聊天室,该聊天室需要定期使用AJAX从PHP检索消息。问题在于用户可以打开不同聊天室的多个选项卡,这将占用服务器大量资源。因此,当用户切换页面时,如何在其他选项卡中停止功能,然后在他们返回到选项卡时对其进行响应。我是编码的新手,因此请尽可能简化代码(请不要使用jQuery。)

这是我正在尝试的功能测试,但是没有运气:

function window_active(){
    window.onfocus = function() {
        test()
    };

        window.onblur = function() {
            //stop the script OR change the setTimeout so the functon run less.
        };
    }

    function test(){
        alert('adadasdad');
        setTimeout(function(){}, 10000);
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢。(:

更新:requestAnimationFrame()无法正常工作。

function loop() {
    var div_id = document.getElementById('tester');
    var msg = document.createTextNode("sadksadkjsahdjkasdjkahdjkas");
    div_id.appendChild(msg);

    setTimeout( function() {
        requestAnimationFrame( function() {
            loop();
        } );
    }, 1000 );
}
Run Code Online (Sandbox Code Playgroud)

更新 2:Counldn在任何地方都找不到此答案,然后我很幸运,并在有关“ document.hidden”的ehynds答案的帮助下找到了此页面。谢谢,谢谢!(:

function loop() {

    //do stuff.

    setTimeout( function() {
        if(document.hasFocus()){
        //"document.hasFocus()" return **true** only if your …
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
693
查看次数

标签 统计

javascript ×3

cryptojs ×1

encryption ×1