Temasys / AdapterJS

AdapterJS Javascript Polyfill and Tools for WebRTC - Skylink WebRTC
http://skylink.io/web
Other
430 stars 100 forks source link

Implementing plugin callback without touching AdapterJS file #69

Open ulifigueroa opened 9 years ago

ulifigueroa commented 9 years ago

If I want to implement theAdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb callback the source code says that it needs to be overridden but what I don't want to do is to end up using a custom AdapterJS version. Also it would be nice to be able to pass options. For example like this:

window.AdapterJS = {
    options: {
        hidePluginInstallPrompt: true
    },
    WebRTCPlugin: {
        pluginNeededButNotInstalledCb: function() {
            //Do something when the plugin is not installed
        }
    }
};
serrynaimo commented 9 years ago

After importing AdapterJS you can just do

AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb = function() {
    // your code
};

This way you don't need to touch the AdapterJS code.

Hope this helps :)

ulifigueroa commented 9 years ago

I forgot to mention that I'm lazy loading AdapterJS this might be causing that at the point where I want to define the callback either AdapterJS is still not defined or the callback was already set to null because of this line https://github.com/Temasys/AdapterJS/blob/master/source/adapter.js#L122

serrynaimo commented 9 years ago

I see. Now it makes more sense to pass it as a config. I'll keep it open for @johache to consider.

Thanks!

johache commented 9 years ago

Hi,

Good points I think, I saw you PR : https://github.com/Temasys/AdapterJS/pull/70/files Just looking at it it looks quite good, but I will need to run tests before validating it. Hopefully I can get to it by Monday (hopefully...)

Thanks

RuneAndersen commented 8 years ago

Sorry but I don't get it :-/

AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb = AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb || function () {...}

I can replace the AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb function with my own function, but it seems to me the original AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb is called immediately after the AdapterJS script loads so my replacement has no effect and I can't figure out to define it before AdapterJS loads, is that possible?.

Could you please provide me with a small working sample of how to intercept the callback method?

@johache @ulifigueroa

johache commented 8 years ago

The callback function is indeed called immediately as AJS in loaded. You need to set AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb BEFORE you include or require AJS.

RuneAndersen commented 8 years ago

@joache thanks got it now :-)

Just nut used to the dynamics of JavaScript, so if anybody else is as n00b as me here you have an example: var AdapterJS = { WebRTCPlugin: { pluginNeededButNotInstalledCb: function () { alert("Plugin needed, I'm in control"); } } };

marifehe commented 7 years ago

Hi, I am having problems related to this issue.. I try to set AdapterJS.options.hidePluginInstallPrompt = true; before requiring AdapterJS, since I don't want to end up with a custom version, but even setting the value to global scope in window (I use strict mode), once the adapter.js is loaded, what is supposed to hold my value, is undefined. I created a PR that solves this issue: it just picks config values from the global object in window: https://github.com/Temasys/AdapterJS/pull/253

I saw there is a PR to set the AdapterJS itself in window, wich seems related, but is not, this is just picking up config values from the global object. Does this make sense?