This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Massively simplified copy paste and fixed lots of issues by using eve…
…nts and stopping twitter from interfering with them
- Loading branch information
Showing
3 changed files
with
13 additions
and
70 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,10 @@ | ||
document.addEventListener("keydown", function(e) { | ||
|
||
// When pressing CTRL+V | ||
if (e.key == "v" && e.ctrlKey || e.key == "v" && e.metaKey) { | ||
// Get the active element | ||
var tweetBox = document.activeElement; | ||
// If it's not a tweet return | ||
if(!tweetBox.id.includes("tweet-box")) { | ||
return | ||
} | ||
|
||
// Get caret position | ||
var range = window.getSelection().getRangeAt(0); | ||
var preCaretRange = range.cloneRange(); | ||
preCaretRange.selectNodeContents(tweetBox); | ||
preCaretRange.setEnd(range.endContainer, range.endOffset); | ||
var pos = preCaretRange.toString().length; | ||
|
||
// Create temporary input field | ||
var tempInput = document.createElement("textarea"); | ||
tempInput.style.position = "absolute"; | ||
tempInput.style.top = "0"; | ||
document.body.appendChild(tempInput); | ||
tempInput.focus(); | ||
|
||
tempInput.addEventListener("input", function(e) { | ||
|
||
tweetBox.focus(); | ||
|
||
// For every character in the temporary input field | ||
tempInput.value.split("").forEach(function(char, i) { | ||
|
||
// Call keydown event | ||
tweetBox.dispatchEvent( | ||
new KeyboardEvent("keydown", { | ||
type: "keydown", | ||
bubbles: true, | ||
cancelable: true, | ||
key: char, | ||
char: char, | ||
keyCode: char.charCodeAt(0), | ||
which: char.charCodeAt(0) | ||
}) | ||
); | ||
|
||
// Add character | ||
var textArr = tweetBox.textContent.split(""); | ||
textArr.splice(pos + i, 0, char); | ||
tweetBox.textContent = textArr.join(""); | ||
|
||
}); | ||
|
||
// Remove all temporary input fields | ||
document.querySelectorAll("textarea").forEach(function(textarea) { | ||
if (textarea.classList.length == 0) textarea.outerHTML = ""; | ||
}); | ||
|
||
// Scroll to the top | ||
setTimeout(function() { | ||
scrollTo(0, 0); | ||
}); | ||
|
||
}); | ||
|
||
} | ||
|
||
}); | ||
// Whenever there's an event for cut/copy/paste stop the propagation of the event | ||
window.document.addEventListener('cut', function (e) { | ||
e.stopPropagation(); | ||
}, true); | ||
window.document.addEventListener('copy', function (e) { | ||
e.stopPropagation(); | ||
}, true); | ||
window.document.addEventListener('paste', function (e) { | ||
e.stopPropagation(); | ||
}, true); |
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