Skip to content

Commit

Permalink
Merge pull request #17 from cxw42/pull-for-14
Browse files Browse the repository at this point in the history
Updates from @Procyon-b: DOMContentLoaded/load timer; name=""
  • Loading branch information
cxw42 authored Sep 24, 2018
2 parents 625afe8 + 8c1c02e commit d0adc93
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ Additional contributors (in alphabetical order):
- [panozzaj](https://github.com/panozzaj)
- [Procyon-b](https://github.com/Procyon-b)
- [rspeed](https://github.com/rspeed)


2 changes: 1 addition & 1 deletion src/bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ chrome.runtime.onMessage.addListener(
text: '1',
tabId: sender.tab.id
});
});
});
77 changes: 59 additions & 18 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,25 @@ const DEBUG=false;
let numseen=0, numspoiled=0;
let unspoiled=[];

// called when the user clicks an element of the form (any field or button). The parameter passed is the event object
function clickApply(e) {
if(DEBUG) console.info({'form onclick':e});
// remove onclick. One fix only
e.srcElement.form.removeEventListener("click", clickApply);
applyFix(e.srcElement.form);
}

// add a new <textarea> element
function applyFix(elem) {
var newelem = document.createElement('textarea');
newelem.name = '';
newelem.style.display='none';
elem.appendChild(newelem);
}

// Add an extra child input to any form that only has one
function spoilFormGet(elem) {
if(DEBUG) {
console.info({Found: elem});
++numseen;
unspoiled.push(elem);
}
Expand All @@ -20,18 +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])').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/
var newelem;
newelem = document.createElement('textarea');
newelem.name = 'chrome_dont_add_custom_search_engines_srsly';
newelem.style.display='none';
elem.appendChild(newelem);

// apply the fix now, or place it in onclick. "this" is a parameter passed by foreach(). see below
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 @@ -40,7 +60,25 @@ function spoilFormGet(elem) {
}
} //spoilFormGet

function main() { // runs on DOMContentLoaded
var debugAutoDetect=0;

// move this part of the code here, since it's called 3 times
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);
}

// we reset spoil vars for next call
numseen=0;
numspoiled=0;
unspoiled=[];
}

function onDOMContentLoaded()
{

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

// Chrome autodetection, https://www.chromium.org/tab-to-search #2 .
window.addEventListener('load', function() {
document.querySelectorAll('form:-webkit-any([method="get" i],:not([method]))').forEach(spoilFormGet);
// #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).

if(DEBUG) {
console.log(`Spoiled ${numspoiled}/${numseen}. Unspoiled were:`);
console.log(unspoiled);
}
});
} //main
window.addEventListener('load', function() { autoDetect(true,'Load'); } ); // #3
setTimeout(function() { autoDetect(true,'Timer'); } ,1500); // #2
autoDetect(false,'onClick'); // #1

} //onDOMContentLoaded

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

// vi: set ts=4 sts=4 sw=4 et ai: //
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Greg Sadetsky et al.",
"manifest_version": 2,
"name": "Don't add custom search engines",
"version": "0.0.4",
"version": "0.0.5",
"description": "Prevent Google Chrome from auto-adding custom search engines",
"minimum_chrome_version": "49.0.0.0",
"offline_enabled": true,
Expand Down

0 comments on commit d0adc93

Please sign in to comment.