Using Tampermonkey on an angularjs site. I'm prepopulating an inputbox with a value whenever I open the url, but the search is triggered by typing. So I need a keydown-event ie "space" to trigger the search.
I've figured out how to trigger a keydown-event, but it overrides the value already in the inputbox. How do I retain it, while still triggering a keydown?
(function() {
'use strict';
var herId = getParameterByName('Q');
window.setTimeout(function() {
var el = document.querySelector('.form-control');
el.value = herId;
}, 200);
window.setTimeout(function() {
var e = jQuery.Event("keydown");
e.which = 32; // # Some key code value
$("input").val(String.fromCharCode(e.which));
$("input").trigger(e);
}, 1600);
})();
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
I'm using the delays because the page takes some time to load.
Every time the keydown event runs, the old value is cleared out and replace by the new String.fromCharCode character.
//Try this
(function() {
'use strict';
var herId = getParameterByName('Q');
window.setTimeout(function() {
var el = document.querySelector('.form-control');
el.value = herId;
}, 200);
window.setTimeout(function() {
var e = jQuery.Event("keydown");
e.which = 32; // # Some key code value
$("input").val($(this).val() + String.fromCharCode(e.which));
$("input").trigger(e);
}, 1600);
})();
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.