Created
May 22, 2014 09:57
-
-
Save DavidWiesner/c0d390621275d50f8fe3 to your computer and use it in GitHub Desktop.
UserScript for opera duckduckgo fix onkeypress not detected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name | |
// @namespace events.script | |
// @include http://duckduckgo.com/* | |
// @include https://duckduckgo.com/* | |
// @match http://duckduckgo.com/* | |
// @match https://duckduckgo.com/* | |
// @run-at document-end | |
// ==/UserScript== | |
(function () { | |
function forwardEvents(){ | |
if(!Array.prototype.contains) Array.prototype.contains = function(obj) { | |
var i = this.length; | |
while (i--) { | |
if (this[i] == obj) { | |
return true; | |
} | |
} | |
return false; | |
} | |
var codes=[ | |
/*ESC:*/ 27, | |
/*TAB:*/ 9, | |
/*RETURN:*/ 13, | |
/*UP:*/ 38, | |
/*DOWN:*/ 40, | |
/*LEFT:*/ 37, | |
/*RIGHT:*/ 39 | |
]; | |
document.onkeydown = function(e){ | |
e = e || window.event; | |
var target = e.target || e.srcElement; | |
if(!codes.contains(e.keyCode)){ | |
return true; | |
} | |
if(document.createEventObject) | |
{ | |
var eventObj = document.createEventObject(); | |
eventObj.keyCode = e.keyCode; | |
target.fireEvent("onkeypress", eventObj); | |
}else if(document.createEvent) | |
{ | |
var eventObj = document.createEvent("Events"); | |
eventObj.initEvent("keypress", true, true); | |
eventObj.which = e.keyCode; | |
eventObj.keyCode = e.keyCode; | |
target.dispatchEvent(eventObj); | |
} | |
return true; | |
}; | |
} | |
var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'; | |
if(!isOpera){ | |
return; | |
} | |
if(document.body){ | |
forwardEvents(); | |
} else { | |
window.addEventListener('DOMContentLoaded',function(e){forwardEvents();}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment