Skip to content

Instantly share code, notes, and snippets.

@Infocatcher
Created July 30, 2013 22:33
Show Gist options
  • Save Infocatcher/6117669 to your computer and use it in GitHub Desktop.
Save Infocatcher/6117669 to your computer and use it in GitHub Desktop.
Click to Play per-element demo for Scratchpad (devtools.chrome.enabled = true, Environment – Browser) or something similar. Based on code from https://addons.mozilla.org/firefox/addon/click-to-play-per-element/
let WindowListener = {
handleEvent: function(aEvent) {
var trg = aEvent.originalTarget;
if( // I don't know, how do better checks here :(
trg.className != "hoverBox"
|| String.toLowerCase(trg.localName) != "div"
|| !trg.parentNode
|| trg.parentNode.className != "mainBox"
//|| String.toLowerCase(aEvent.target.localName) != "embed"
|| !(aEvent.target instanceof Ci.nsIObjectLoadingContent)
)
return;
var window = aEvent.currentTarget;
var document = window.document;
let plugin = document.getBindingParent(trg);
_log("plugin: " + plugin);
let contentWindow = plugin.ownerDocument.defaultView.top;
// gBrowser.getBrowserForDocument does not exist in the case where we
// drag-and-dropped a tab from a window containing only that tab. In
// that case, the window gets destroyed.
let browser = window.gBrowser.getBrowserForDocument ?
window.gBrowser.getBrowserForDocument(contentWindow.document) :
null;
// If browser is null here, we've been drag-and-dropped from another
// window, and this is the wrong click handler.
if (!browser) {
//aEvent.target.removeEventListener("click", window.gPluginHandler._overlayClickListener, true);
return;
}
let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
// Have to check that the target is not the link to update the plugin
if (!(aEvent.originalTarget instanceof window.HTMLAnchorElement) &&
(aEvent.originalTarget.getAttribute('anonid') != 'closeIcon') &&
aEvent.button == 0 && aEvent.isTrusted) {
if (objLoadingContent.pluginFallbackType ==
Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_UPDATABLE ||
objLoadingContent.pluginFallbackType ==
Ci.nsIObjectLoadingContent.PLUGIN_VULNERABLE_NO_UPDATE)
window.gPluginHandler._showClickToPlayNotification(browser, true);
else
this.activateSinglePlugin(window, contentWindow, plugin);
aEvent.stopPropagation();
aEvent.preventDefault();
}
},
activateSinglePlugin: function PH_activateSinglePlugin(window, aContentWindow, aPlugin) {
_log("activateSinglePlugin()");
let objLoadingContent = aPlugin.QueryInterface(Ci.nsIObjectLoadingContent);
if (window.gPluginHandler.canActivatePlugin(objLoadingContent))
objLoadingContent.playPlugin();
let cwu = aContentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
let pluginNeedsActivation = this._pluginNeedsActivationExceptThese(window, [aPlugin]);
let browser = window.gBrowser.getBrowserForDocument(aContentWindow.document);
let notification = window.PopupNotifications.getNotification("click-to-play-plugins", browser);
if (notification) {
notification.remove();
}
if (pluginNeedsActivation) {
window.gPluginHandler._showClickToPlayNotification(browser);
}
},
_pluginNeedsActivationExceptThese: function PH_pluginNeedsActivationExceptThese(window, aExceptThese) {
let contentWindow = window.gBrowser.selectedBrowser.contentWindow;
let cwu = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
let pluginNeedsActivation = cwu.plugins.some(function (plugin) {
let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
return (window.gPluginHandler.canActivatePlugin(objLoadingContent) &&
aExceptThese.indexOf(plugin) < 0);
});
return pluginNeedsActivation;
}
};
window.addEventListener("click", WindowListener, true);
setTimeout(function() {
window.removeEventListener("click", WindowListener, true);
_log("!!! Stop !!!");
}, 10e3);
function _log(s) {
Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService)
.logStringMessage("[c2p]: " + s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment