Using javascript for pinging a webapp to keep session open

前端 未结 6 715
一个人的身影
一个人的身影 2021-01-31 10:26

I\'m writing a greasemonkey script to keep session open on a webapp I use for work. Which javascript command would you use to create some feedback with the server and ensure the

相关标签:
6条回答
  • 2021-01-31 10:48

    I've solved the issue using:

    function keepAlive() {
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', "/restricted_file_url");
        httpRequest.send(null);
    }
    
    setInterval(keepAlive, 840000);  //My session expires at 15 minutes
    
    0 讨论(0)
  • 2021-01-31 10:52

    Using some tips from Jesse's answer, here's the complete code.

    You will need to update the @match param with your site's domain

    // ==UserScript==
    // @name         Auto session extender
    // @namespace    http://obive.net/
    // @version      0.2
    // @description  Automatically extend server-side session
    // @author       Charlie Hayes
    // @match        http://obive.net/
    // @grant   GM_getValue
    // @grant   GM_setValue
    // @noframes
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        console.log('The session for this site will be extended automatically via userscript.');
    
        var minute = 60*1000;
        var refreshTime = 15 * minute;
    
        var iframe = document.createElement("iframe");
        iframe.style.width = 0;
        iframe.style.height=0;
    
        var loc = window.location;
        var src = loc.protocol +'//' + loc.host + loc.pathname;
        src += loc.search ? (loc.search + '&') : '?';
        src +=  'sessionextendercachebuster=';
    
        var reloadIframe = function(){
            var time = new Date().getTime();
            var lastRefresh = GM_getValue('lastRefresh');
            var timeSinceLastRefresh = time - lastRefresh;
            if (!lastRefresh || timeSinceLastRefresh > refreshTime - minute) {
                console.log('Auto-extending session');
                iframe.src = src + time;
                GM_setValue('lastRefresh',time);
                setTimeout(reloadIframe, refreshTime);
    
                setTimeout(function(){
                    // Unload the iframe contents since it might be taking a lot of memory
                    iframe.src='about:blank';
                },10000);
            }else{
                console.log('Another tab/window probably refreshed the session, waiting a bit longer');
                setTimeout(reloadIframe, refreshTime - timeSinceLastRefresh - minute);
            }
        };
        setTimeout(reloadIframe, refreshTime);
        document.body.appendChild(iframe);
    })();
    
    0 讨论(0)
  • 2021-01-31 10:58

    Second choice

    If you absolutely insist on using Greasemonkey, any element.click() method on any event that submits an XMLHTTPrequest should do.

    First choice

    If you are willing to use a solution that does not require you to write a Greasemonkey script:

    ReloadEvery 3.0.0, by Jaap Haitsma

    This reloads web pages every so many seconds or minutes. The function is accessible via the context menu (the menu you get when you right click on a web page) or via a drop down menu on the reload button.

    It's not what you asked for, but unless you simply want to brush up on your javascript skills, this is probably the most straight-forward method for solving your problem.

    0 讨论(0)
  • 2021-01-31 11:06

    This is presuming you don't want to keep the page constantly refreshing, as suggested by others.

    Ordinarily, if you were simply looking to ping the server, you would probably do best to do something like use an image that you know exists on the remote server, and check it's height - thus creating traffic.

    However, your problem (I presume) needs to access some limited area of the website, in order to keep the session active... with this being the case (and the fact that you can't directly ping), find a page that is 'restricted' and do an Ajax call, or similar. Don't quote me on this, as I'm by no means a JavaScript expert... just throwing out a potential solution.

    0 讨论(0)
  • 2021-01-31 11:07

    If you want to write JavaScript to solve this (the other answers are a lot easier and I would recommend going the easier route), here's what I would do:

    • Call document.createElement("iframe") to create an iframe
    • Size the iframe to 0x0, style.display = "none", set the ID to something, and set the src property to a page on the site that will extend your session
    • Use document.body.appendChild to add the iframe to the page
    • Using setTimeout refresh the iFrame using window.frames['iframeID'].location.reload(true); and also call setTimeout to refresh the page again.

    See if the other answers would work because using JavaScript seems like it would be more trouble than it's worth.

    0 讨论(0)
  • 2021-01-31 11:08

    I can definitely recommend the solution suggested by dreftymac!

    If you don't want to worry about remembering to click the reloadevery option, your grease monkey script is simply

    window.location.href = window.location.href
    
    0 讨论(0)
提交回复
热议问题