Skip to content

Commit

Permalink
Fixes per discussion in #16
Browse files Browse the repository at this point in the history
Updated CSS selector, and using === with this.now in spoilFormGet
  • Loading branch information
Procyon-b authored and Chris White committed Aug 9, 2018
1 parent a530810 commit 58a4c60
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/content.js 100755 → 100644
Expand Up @@ -21,7 +21,6 @@ function applyFix(elem) {
// Add an extra child input to any form that only has one
function spoilFormGet(elem) {
if(DEBUG) {
// cleaning debug output. comment out, this is displayed in "unspoiled": //console.info({t:this, Found: elem});
++numseen;
unspoiled.push(elem);
}
Expand All @@ -36,22 +35,23 @@ function spoilFormGet(elem) {
// Autodetection requires exactly one input of type text or search
// If the type attribute is missing, it defaults to `text`
// Readonly inputs do not count against this total
if(elem.querySelectorAll(':scope input:-webkit-any([type="text" i],[type="search" i],:not([type])):not([readonly])[name]').length !== 1) return;
if(elem.querySelectorAll(':scope input:-webkit-any([type="text" i],[type="search" i],:not([type])):not([readonly])[name]:not([name=""])').length !== 1) return;

// Autodetection also requires no password, file, or textarea elements
if(elem.querySelector(':scope :-webkit-any(input[type="password" i],input[type="file" i],textarea)')) return;

// Add a <textarea> - unlike <input>, it doesn't block implicit submission
// per https://www.tjvantoll.com/2013/01/01/enter-should-submit-forms-stop-messing-with-that/

// apply the fix now, or place it in onclick. "this" is a parameter passed by foreach(). see below
if (this == true) {
// remove onclick placed during first pass
elem.removeEventListener("click", clickApply);
// and instead do it now;
applyFix(elem);
}
else elem.addEventListener('click', clickApply);
if (this.now === true) {
// remove onclick placed during first pass
elem.removeEventListener("click", clickApply);
// and instead do it now;
applyFix(elem);
} else {
elem.addEventListener('click', clickApply);
}

if(DEBUG) {
console.info({Spoiled: elem});
Expand All @@ -60,11 +60,12 @@ function spoilFormGet(elem) {
}
} //spoilFormGet

var debugAutoDetect=0
var debugAutoDetect=0;

// move this part of the code here, since it's called 3 times
function autoDetect(now,cmt) {
if(DEBUG) console.log('autoDetect: '+(++debugAutoDetect)+' ('+cmt+')')
document.querySelectorAll('form:-webkit-any([method="get" i],:not([method]))').forEach(spoilFormGet,now);
function autoDetect(now, when_called) {
if(DEBUG) console.log('autoDetect: '+(++debugAutoDetect)+' ('+when_called+')');
document.querySelectorAll('form:-webkit-any([method="get" i],:not([method]))').forEach(spoilFormGet,{now});
if(DEBUG) {
console.log(`Spoiled ${numspoiled}/${numseen}.`+(unspoiled.length?' Unspoiled were:':'') );
if (unspoiled.length) console.log(unspoiled);
Expand All @@ -76,7 +77,8 @@ function autoDetect(now,cmt) {
unspoiled=[];
}

function main() { // runs on DOMContentLoaded
function onDOMContentLoaded()
{

// OpenSearch - e.g., https://martin-thoma.com/search-engine-autodiscovery/
// Uses CSS4 selectors, Chrome 49+
Expand All @@ -95,22 +97,20 @@ function main() { // runs on DOMContentLoaded
}
);

// #1 call it now without applying the fix
// #2 call it in 1500 ms and apply the fix
// #1 call it now (i.e., DOMContentLoaded) without applying the fix
// #2 call it in 1500 ms and apply the fix
// #3 call when document loaded, and apply the fix
// if <form> is added/modified dynamically before the document is fully loaded #1 could miss it,
// but not #2 & #3. Note that #2 could fire after #3 if the page is fast to load.
// Once the fix is applied, the <form> can't be found by subsequent execution of autoDetect,
// so the fix can only be applied once (#1 is not applied but delayed until #2 or #3 fires, or if the user clicks).

window.addEventListener('load', function() { autoDetect(true,'Load'); } );
setTimeout(function() { autoDetect(true,'Timer'); } ,1500);
autoDetect(false,'onClick');

} //main
window.addEventListener('load', function() { autoDetect(true,'Load'); } ); // #3
setTimeout(function() { autoDetect(true,'Timer'); } ,1500); // #2
autoDetect(false,'onClick'); // #1

var cc=0;
} //onDOMContentLoaded

document.addEventListener('DOMContentLoaded', main);
document.addEventListener('DOMContentLoaded', onDOMContentLoaded);

// vi: set ts=4 sts=4 sw=4 et ai: //

0 comments on commit 58a4c60

Please sign in to comment.