Last active
December 15, 2015 15:29
-
-
Save stefek99/5282247 to your computer and use it in GitHub Desktop.
Playing around with Chrome Extensions - Workflowy on steroids (ma favourite web app)
This file contains 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
console.log("Using included jQuery! " + document.location.href + " " + jQuery.fn.jquery); | |
var $nativeInput; | |
var totalTextareas = 2; | |
var eventsAssigned = 0; | |
$(document).on("keydown.textarea", "textarea", function(e) { | |
console.log("window keydown: " + e.keyCode); | |
var $maybeTextarea = $(document.activeElement); | |
if ($maybeTextarea.length > 0 && | |
$maybeTextarea.is("textarea") && | |
$maybeTextarea.closest(".lastEdited").length > 0) { | |
console.log("OK, textarea has focus") | |
$nativeInput = $maybeTextarea; | |
if(! ($nativeInput.data("events") != null && | |
$nativeInput.data("events").keydown != null && | |
$nativeInput.data("events").keydown[0].namespace == "native")) | |
{ | |
eventsAssigned++; | |
$nativeInput.on("keydown.native", function(e) { | |
if (e.altKey && e.keyCode == 13) { | |
$customTextarea.focus(); | |
} | |
}); | |
} | |
if (eventsAssigned == totalTextareas) { | |
$(document).off("keydown.textarea"); | |
} | |
} | |
}); | |
$("textarea").css("border", "2px solid blue"); | |
$customTextarea = $("<textarea id='mine'>").css({"border": "2px solid red", "position" : "absolute", "top": "40px"}).insertAfter("#logo"); | |
$customTextarea.on("keydown.mine", function(e) { | |
console.log(e.altKey); | |
if (e.altKey && e.keyCode == 13) { | |
tranformInputToNative(); | |
} | |
if (e.keyCode == 81) { | |
$(document).trigger(e); | |
} | |
}); | |
function tranformInputToNative() { | |
text = $customTextarea.val(); | |
$nativeInput.focus(); | |
$nativeInput.val(text); | |
var spaceEvent = createEvent(32); | |
$nativeInput.trigger(spaceEvent); | |
} | |
function createEvent(charCode) { | |
var e = $.Event("keydown"); | |
e.which = charCode; | |
e.keyCode = charCode; | |
return e; | |
} | |
function whoHasFocus() { | |
if ($nativeInput != null) { | |
a = $nativeInput.get(0); | |
b = document.activeElement; | |
console.log("nativeInput == activeElement: " + (a==b)); | |
} | |
} | |
var interval = setInterval("whoHasFocus()", 2000); | |
$(document).on("keydown", function(e) { | |
//console.log("DOCUMENT handler: " + e.keyCode) | |
if (e.keyCode == 81) {// q | |
if (interval != null) { | |
console.log("CLEAR INTERVAL"); | |
clearInterval(interval); | |
interval = null; | |
} else { | |
console.log("RESTORING INTERVAL"); | |
interval = setInterval("whoHasFocus()", 2000); | |
} | |
} | |
}); |
This file contains 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
{ | |
"manifest_version": 2, | |
"name": "Content Script Cross-Domain XMLHttpRequest Example", | |
"version": "2.0.0", | |
"description": "Demonstrates making cross domain requests from a content script by putting Twitter trends on Google News.", | |
"permissions": [ | |
"https://api.twitter.com/*" | |
], | |
"icons": { | |
"48" : "sample-48.png", | |
"128" : "sample-128.png" | |
}, | |
"content_scripts": [ | |
{ | |
"matches": ["http://news.google.com/*", "https://workflowy.com/*"], | |
"js" : ["jquery.js", "contentscript.js"] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment