Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
Massively simplified copy paste and fixed lots of issues by using eve…
Browse files Browse the repository at this point in the history
…nts and stopping twitter from interfering with them
  • Loading branch information
zusorio committed Jul 25, 2019
1 parent bab3e68 commit d305e0b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 70 deletions.
4 changes: 2 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function rewriteUserAgentHeader(e) {
return {requestHeaders: e.requestHeaders};
}

function removeCookie() {
function clearCache() {
browser.browsingData.remove({"hostnames": ["twitter.com"]}, {"cache": true});
browser.tabs.query({url: "*://*.twitter.com/*"}, function (result) {
result.forEach(function (tab) {
Expand All @@ -26,4 +26,4 @@ browser.webRequest.onBeforeSendHeaders.addListener(
["blocking", "requestHeaders"]
);

browser.runtime.onInstalled.addListener(removeCookie);
browser.runtime.onInstalled.addListener(clearCache);
77 changes: 10 additions & 67 deletions fix-paste.js
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);
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Forces your browser to use old twitter.",
"manifest_version": 2,
"name": "GoodTwitter",
"version": "1.7",
"version": "1.8",

"permissions": [
"webRequest",
Expand Down

0 comments on commit d305e0b

Please sign in to comment.