bsorrentino / cordova-broadcaster

Cordova Plugin to allow message exchange between javascript and native (and viceversa)
MIT License
113 stars 53 forks source link

Receive broadcast data from external barcode scanner #67

Closed ghost closed 1 month ago

ghost commented 2 years ago

Hi, is it possible to retrieve the data globally (because i have many input from the pages) when a hardware scanner captured the barcode? I have tested the sample code from the documentation but it does not work.

bsorrentino commented 2 years ago

Hi, is it possible to retrieve the data globally (because i have many input from the pages) when a hardware scanner captured the barcode? I have tested the sample code from the documentation but it does not work.

Hi @JustonJ99

I need more details.

kmitdebus commented 2 months ago

I think I get the same error ("its not working with external reader")

My Code:

window.broadcaster.addEventListener( 'de.someExternalBroadcast.SCAN_DATA', true, function( e ) { console.log( e ); });

broadcaster.js:84 Uncaught TypeError: Cannot set property type of #<Event> which has only a getter at Broadcaster.fireEvent (broadcaster.js:84:30) at <anonymous>:1:20

Broadcasted data is: {data: "25C6AE3E", secondaryData: null, type: "#41", typeExt: "HiTag1/S"}

bsorrentino commented 2 months ago

Hi @kmitdebus thanks for sharing infos, I'll investigate as soon as possible

lgl017 commented 1 month ago

I think I get the same error ("its not working with external reader")

My Code:

window.broadcaster.addEventListener( 'de.someExternalBroadcast.SCAN_DATA', true, function( e ) { console.log( e ); });

broadcaster.js:84 Uncaught TypeError: Cannot set property type of #<Event> which has only a getter at Broadcaster.fireEvent (broadcaster.js:84:30) at <anonymous>:1:20

Broadcasted data is: {data: "25C6AE3E", secondaryData: null, type: "#41", typeExt: "HiTag1/S"}

I get the same error too, i find the issue in js code:

Broadcaster.prototype.fireEvent = function (type, data) {
        if (!this._channelExists(type))
            return;
        var event = document.createEvent('Event');
        event.initEvent(type, false, false);
        if (data) {
            for (var i in data) {
                if (data.hasOwnProperty(i)) {
                    event[i] = data[i];
                  //^^^ will error when key is "type", "type" in event object is readonly
                }
            }
        }
        this._channelFire(event);
    };

to fix that, special handling key "type" to another

bsorrentino commented 1 month ago

Hi @lgl017 thanks for valuable feedback

Now there is another issue .... what's the best way to handle such problem:

  1. catch error and ignore it ( simplest )
  2. rename data.type property in event.dataType
  3. throw a more meaningful exception

I prefer the n. 2 what do you think ?

bsorrentino commented 1 month ago

I think I get the same error ("its not working with external reader") My Code: window.broadcaster.addEventListener( 'de.someExternalBroadcast.SCAN_DATA', true, function( e ) { console.log( e ); }); broadcaster.js:84 Uncaught TypeError: Cannot set property type of #<Event> which has only a getter at Broadcaster.fireEvent (broadcaster.js:84:30) at <anonymous>:1:20 Broadcasted data is: {data: "25C6AE3E", secondaryData: null, type: "#41", typeExt: "HiTag1/S"}

I get the same error too, i find the issue in js code:

Broadcaster.prototype.fireEvent = function (type, data) {
       if (!this._channelExists(type))
           return;
       var event = document.createEvent('Event');
       event.initEvent(type, false, false);
       if (data) {
           for (var i in data) {
               if (data.hasOwnProperty(i)) {
                   event[i] = data[i];
                 //^^^ will error when key is "type", "type" in event object is readonly
               }
           }
       }
       this._channelFire(event);
   };

to fix that, special handling key "type" to another

I've updated the fireEvent code

Broadcaster.prototype.fireEvent = function (type, data) {
        if (!this._channelExists(type))
            return;
        var event = new Event(type, { bubbles: false, cancelable: false });
        if (data) {
            event['data$'] = data; // fix issue #67
            // for backward compatibility
            for (var i in data) {
                if (data.hasOwnProperty(i) && event[i] === undefined) {
                    event[i] = data[i];
                }
            }
        }
        this._channelFire(event);
}

The solution is to store the given data object in a new Event.data$ property. For backward compatibility the data properties will be added to Event object skipping the ones already present in Event itself. So in the case of type clashing to retrieve type value we have to access the data property:

// in the listener 
    var listener = function( e ) {
      console.log( "type:  ", e.data$.type  );
    }
bsorrentino commented 1 month ago

fix released in 5.2.0