javascriptfirefox-addonfirefox-addon-sdkjsctypes

Firefox add-on execute only in debugging state


I have developed an add-on to communicate with a smart card. I have used winscard.dll and its functions (such as Establishment, Connecting, Transmitting).

//less-privileged scope like jsp 

var element = document.createElement("MyExt1");
document.documentElement.appendChild(element);
var evt = document.createEvent("Events");
evt.initEvent("SCardConnect", true,false);
element.dispatchEvent(evt);
var CardHandle = element.getAttribute("CardHandle");
alert(CardHandle);

and

//privileged scope which exist in my add-on

.
.
.
var MyExtension1 = {
    Connect : function(evt){
        ...
        evt.target.setAttribute("CardHandle", CH.toString());
        var doc = evt.target.ownerDocument;
        var AnswerEvt = doc.createElement("SCardConnect");
        doc.documentElement.appendChild(AnswerEvt);
        var event = doc.createEvent("HTMLEvents");
        event.initEvent("ConnectEvent",true,false);
        AnswerEvt.dispatchEvent(event);
    }
}    
.
.
.
document.addEventListener("SCardConnect", function(e){myExtension1.Connect(e);}, false, true);

After a small introduction, this is my problem:

When I install the add-on in Firefox and debug the code step by step through F10 it works fine, however if I want to run the external script without interruption (without debugging), it returns null when I get attributes.

This is an event-based approach to call an add-on function from an external script function. There is another approach that used export function which I get following problem: https://stackoverflow.com/questions/32450103/calling-a-firefox-add-on-function-from-an-external-javascript-file


Solution

  • You might want to move 'var CardHandle = element.getAttribute("CardHandle");' into a new function and check if its value has been valid or not in specified intervals.

    var varTimer = setInterval(function(){ myTimer() }, 1000);
    
    function myTimer() {
        var CardHandle = element.getAttribute("CardHandle");
        if(CardHandle is valid) stopTimer();
    }
    
    function stopTimer() {
        clearInterval(varTimer);
    }