1

I have an accepted answer but am open to better ones

Be gentle, this is my first question here

I am building a web browser plugin to automate a series of processes for my organization. I pretty much have everything handled but this one problem that I have been stuck on for two days... and its like really simple I think.

In a lookup field in our ERP, you must press Shift + Right to cycle through certain options.

I am attempting to trigger this or send this any kind of way that I can.

Jquery version 2.0.3

  • The required event only fires on key up
  • The required event is not firing when I simulate input
  • I suspect it needs to be targeted at the input, or perhaps its related to not being trusted/being simulated
  • It occurs to me, that as I am using a browser extension, perhaps it is something I can simulate from this? I dont know if thats a good way to put it... I wonder what the extension could do here that plain JS or JQ on a page could not.
  • Update(Dec 11): So per above thought, I am looking into modifying the Jquery framework that is being used. I have saved a local copy as an local override and used these two resources to implement.

  • Override Javascript file in chrome

  • https://www.ghacks.net/2018/02/27/make-permanent-changes-to-web-pages-with-chromes-overrides-dev-tool/

  • I am in process of determining if local overrides are persistent (Edit: they appear to be after restart of computer, lets see if its a long term solution) but I was able to console.log my code and see it in the console.

  • Next steps will be modifying the handler to perform the actions I need. and/or feed the information I want fed to the system.

  • Update(Dec 12): probably dont need to override the whole file with this answer how to override a javascript function

Here is my function that triggers the event handler (with no result) which was modified from here

function simulateKey (currentTarget, isTrusted, key, code, keyCode, type, modifiers) {
    var evtName = (typeof(type) === "string") ? "key" + type : "keydown";   
    var modifier = (typeof(modifiers) === "object") ? modifier : {};
    console.log("In simulate key function");
    var event = document.createEvent("HTMLEvents");
    event.initEvent(evtName, true, false);
    event.keyCode = keyCode;
    event.key = key;
    event.code = code;
    event.isTrusted = isTrusted;
    event.Target = currentTarget;

    for (var i in modifiers) {
        event[i] = modifiers[i];
    }

    document.dispatchEvent(event);
}

Here is how I use it (a little hardcoded at the moment, just for testing purposes)

function changeLookup(Lookup) {
    console.log("Change Lookup");

    var event_object = Lookup;
    console.log("Key Event Firing");
    $("input[data-name='Entity.Customer.Key']").focus();
    simulateKey(Lookup, true, "ArrowRight", "ArrowRight", 39, "up", {shiftKey: true });
    console.log("Key Event Fired");
}

I have looked at or tried the following solutions

I have reviewed the following documentation

I am at my wits end here...

Sloppyjosh
  • 11
  • 3

1 Answers1

0

The answer was found here Keydown Simulation in Chrome fires normally but not the correct key

I modified it to keyup and juggled some of the parameters so the shift key was pressed... Seems like OP in the other thread did some strange stuff with the parameters in such a way as to press ctrl and shift... So I fixed that for the initKeyboardEvent but not for initKeyEvent

keyUpEventSim = {};
    keyUpEventSim.keyup = function(target, k, ctrlKey, altKey, shiftKey, metaKey) {
        var oEvent = document.createEvent('KeyboardEvent');

        // Chromium Hack
        Object.defineProperty(oEvent, 'keyCode', {
                    get : function() {
                        return this.keyCodeVal;
                    }
        });     
        Object.defineProperty(oEvent, 'which', {
                    get : function() {
                        return this.keyCodeVal;
                    }
        });     

        if (oEvent.initKeyboardEvent) {
            oEvent.initKeyboardEvent("keyup", true, false, document.defaultView, false, false, ctrlKey, altKey, shiftKey, metaKey);
        } else {
            oEvent.initKeyEvent("keyup", true, false, document.defaultView, false, false, true, false, k, 0);
        }

        oEvent.keyCodeVal = k;

        if (oEvent.keyCode !== k) {
            alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
        }

        target.dispatchEvent(oEvent);
    }

called it as follows

function changeLookup(Lookup) {
    Lookup.focus();
    keyUpEventSim.keyup(Lookup,39, false, false, true, false);
}
Sloppyjosh
  • 11
  • 3