MozillaZine

chrome can't detect indirect click in content (Fx 56-)

Talk about add-ons and extension development.
pintassilgo
 
Posts: 125
Joined: August 30th, 2013, 3:50 pm

Post Posted September 6th, 2017, 7:02 pm

I want to detect clicks in file upload button (<input type='file'>), but some sites use different elements to indirectly click the hidden input-file element.

For example:
Code: Select all
<a id="upload" onclick="document.getElementById('choose_photo_upload').click();">Upload</a>
<input id="choose_photo_upload" style="visibility: hidden;" type="file">

If I use this code, OK:
Code: Select all
content.document.addEventListener('click', function(e) {
  if (e.target.tagName == 'INPUT' && e.target.type == 'file') {
    console.log('Input file clicked!');
  }
})

But content is specific to the selected tab. This way I will need to add a addEventListener on each tab. I would like to add a single event listener on chrome, so I replaced content.document by document.getElementById('browser'). But this doesn't work, the event is only triggered by the directly clicked element (the "upload" link, in this example), not the hidden input indirectly clicked. What am I doing wrong?

(This is for a userChromeJS script to avoid bug 918780. The code
Code: Select all
// ==UserScript==
// @name         popupBlockerDoesntBreakFileUpload.uc.js
// @include      main
// ==/UserScript==

document.getElementById('browser').addEventListener('click', function(e) {
  if ((e.target.tagName == 'INPUT' && e.target.type == 'file') || (e.target.tagName == 'LABEL' && e.target.hasAttribute('for') && content.document.getElementById(e.target.getAttribute('for')))) {
    Services.prefs.clearUserPref('dom.popup_allowed_events');
    setTimeout( function () { Services.prefs.setCharPref('dom.popup_allowed_events', ''); }, 0);
  }
});

works on Imgur, but doesn't work on Google Drive, for example.)

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests