From d305e0b5c2724b2610d99d524cc919626b8816c3 Mon Sep 17 00:00:00 2001 From: Tobias Messner Date: Thu, 25 Jul 2019 11:57:01 -0600 Subject: [PATCH] Massively simplified copy paste and fixed lots of issues by using events and stopping twitter from interfering with them --- background.js | 4 +-- fix-paste.js | 77 +++++++-------------------------------------------- manifest.json | 2 +- 3 files changed, 13 insertions(+), 70 deletions(-) diff --git a/background.js b/background.js index 8cf4c92..e4b740c 100644 --- a/background.js +++ b/background.js @@ -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) { @@ -26,4 +26,4 @@ browser.webRequest.onBeforeSendHeaders.addListener( ["blocking", "requestHeaders"] ); -browser.runtime.onInstalled.addListener(removeCookie); \ No newline at end of file +browser.runtime.onInstalled.addListener(clearCache); \ No newline at end of file diff --git a/fix-paste.js b/fix-paste.js index 5169bc3..1aa193e 100644 --- a/fix-paste.js +++ b/fix-paste.js @@ -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); diff --git a/manifest.json b/manifest.json index ebdd568..0c95461 100644 --- a/manifest.json +++ b/manifest.json @@ -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",