Skip to content

Commit

Permalink
Linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pento committed Nov 19, 2024
1 parent c20dea1 commit 9f0ddc5
Showing 1 changed file with 48 additions and 89 deletions.
137 changes: 48 additions & 89 deletions src/js/content.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
let shiftPressed = false;

const isFirefox = navigator.userAgent.toLowerCase().includes("firefox");
const isFirefox = navigator.userAgent.toLowerCase().includes('firefox');

function pasteHandler(event) {
if (shiftPressed) {
return;
}

if (typeof event.clipboardData === "undefined") {
if (event.clipboardData === undefined) {
return;
}

const pasted = event.clipboardData.getData("text/plain");
if (pasted === "") {
const pasted = event.clipboardData.getData('text/plain');
if (pasted === '') {
return;
}

if (!pasted.match(/^https?:\/\//i)) {
if (!/^https?:\/\//i.test(pasted)) {
return;
}

Expand All @@ -28,24 +28,22 @@ function pasteHandler(event) {

if (editor.isContentEditable) {
event.preventDefault();
document.execCommand("createLink", false, pasted);
document.execCommand('createLink', false, pasted);
return;
}

let inputTypeAllowed = false;

if (editor.nodeName === "TEXTAREA") {
if (editor.nodeName === 'TEXTAREA') {
inputTypeAllowed = true;
}

// Normally we don't want to do this on <input> tags, but there are exceptions.
const inputElementSites = ["teuxdeux.com"];
const inputElementSites = [
'teuxdeux.com',
];

if (
editor.nodeName === "INPUT" &&
editor.type === "text" &&
inputElementSites.some((site) => document.domain.includes(site))
) {
if (editor.nodeName === 'INPUT' && editor.type === 'text' && inputElementSites.some(site => document.domain.includes(site))) {
inputTypeAllowed = true;
}

Expand All @@ -65,35 +63,32 @@ function pasteHandler(event) {
}

// If the current selection is also a URL, assume we want to replace it (not wrap it in an anchor)
if (editor.value.slice(start, end).match(/^https?:\/\/\S*$/i)) {
if (/^https?:\/\/\S*$/i.test(editor.value.slice(start, end))) {
return;
}

const markdownSites = [
"hackerone.com",
"github.com",
"reddit.com",
"teuxdeux.com",
"trello.com",
'hackerone.com',
'github.com',
'reddit.com',
'teuxdeux.com',
'trello.com',
];

let bbCodeElement = document.querySelector("#disable_bbcode");
if (!bbCodeElement) {
bbCodeElement = document.querySelectorAll(".show_bbcode").item(0);
}
let bbCodeElement = document.querySelector('#disable_bbcode');
bbCodeElement ||= document.querySelectorAll('.show_bbcode').item(0);

const tracForm = document.querySelector("#propertyform");
if (
tracForm &&
(tracForm.getAttribute("action").includes("/newticket") ||
tracForm.getAttribute("action").includes("/ticket/"))
const tracForm = document.querySelector('#propertyform');
if (tracForm
&& (tracForm.getAttribute('action').includes('/newticket')
|| tracForm.getAttribute('action').includes('/ticket/'))
) {
pasteTrac(event, editor, pasted, start, end);
} else if (markdownSites.some((site) => document.domain.includes(site))) {
} else if (markdownSites.some(site => document.domain.includes(site))) {
pasteMarkdown(event, editor, pasted, start, end);
} else if (bbCodeElement) {
pasteBBcode(event, editor, pasted, start, end);
} else if (editor.classList.contains("remarkup-assist-textarea")) {
} else if (editor.classList.contains('remarkup-assist-textarea')) {
pasteRemarkup(event, editor, pasted, start, end);
} else {
pasteHTML(event, editor, pasted, start, end);
Expand All @@ -106,27 +101,16 @@ function shiftChecker(event) {

function pasteTrac(event, editor, pasted, start, end) {
event.preventDefault();
insertText(
"[" + pasted + " " + editor.value.slice(start, end) + "]",
editor,
start,
end,
);
insertText('[' + pasted + ' ' + editor.value.slice(start, end) + ']', editor, start, end);

const newPos =
start === end ? start + pasted.length + 2 : end + pasted.length + 3;
const newPos = start === end ? start + pasted.length + 2 : end + pasted.length + 3;

editor.setSelectionRange(newPos, newPos);
}

function pasteMarkdown(event, editor, pasted, start, end) {
event.preventDefault();
insertText(
"[" + editor.value.slice(start, end) + "](" + pasted + ")",
editor,
start,
end,
);
insertText('[' + editor.value.slice(start, end) + '](' + pasted + ')', editor, start, end);

const newPos = start === end ? start + 1 : end + pasted.length + 4;

Expand All @@ -135,30 +119,18 @@ function pasteMarkdown(event, editor, pasted, start, end) {

function pasteBBcode(event, editor, pasted, start, end) {
event.preventDefault();
insertText(
"[url=" + pasted + "]" + editor.value.slice(start, end) + "[/url]",
editor,
start,
end,
);
insertText('[url=' + pasted + ']' + editor.value.slice(start, end) + '[/url]', editor, start, end);

const newPos =
start === end ? start + pasted.length + 6 : end + pasted.length + 12;
const newPos = start === end ? start + pasted.length + 6 : end + pasted.length + 12;

editor.setSelectionRange(newPos, newPos);
}

function pasteRemarkup(event, editor, pasted, start, end) {
event.preventDefault();
insertText(
"[[ " + pasted + " | " + editor.value.slice(start, end) + " ]]",
editor,
start,
end,
);
insertText('[[ ' + pasted + ' | ' + editor.value.slice(start, end) + ' ]]', editor, start, end);

const newPos =
start === end ? start + pasted.length + 6 : end + pasted.length + 9;
const newPos = start === end ? start + pasted.length + 6 : end + pasted.length + 9;

editor.setSelectionRange(newPos, newPos);
}
Expand All @@ -168,71 +140,58 @@ function pasteHTML(event, editor, pasted, start, end) {
const leftString = editor.value.slice(0, start).toLowerCase();

// If we are inside a (start) tag for any HTML element, its not ok to paste as a an a-href
if (
leftString.lastIndexOf("<") > -1 &&
leftString.lastIndexOf("<") > leftString.lastIndexOf(">")
) {
if ((leftString.includes('<')) && (leftString.lastIndexOf('<') > leftString.lastIndexOf('>'))) {
return;
}

// If we are inside an anchor's content, its not ok to paste as a an a-href
if (
leftString.lastIndexOf("<a") > -1 &&
leftString.lastIndexOf("<a") > leftString.lastIndexOf("</a>")
) {
if ((leftString.includes('<a')) && (leftString.lastIndexOf('<a') > leftString.lastIndexOf('</a>'))) {
return;
}

// Looks safe, let's do this.
event.preventDefault();
insertText(
'<a href="' + pasted + '">' + editor.value.slice(start, end) + "</a>",
editor,
start,
end,
);
insertText('<a href="' + pasted + '">' + editor.value.slice(start, end) + '</a>', editor, start, end);

const newPos =
start === end ? start + pasted.length + 11 : end + pasted.length + 15;
const newPos = start === end ? start + pasted.length + 11 : end + pasted.length + 15;

editor.setSelectionRange(newPos, newPos);
}

function insertText(text, editor, start, end) {
if (isFirefox) {
editor.value =
editor.value.slice(0, start) + text + editor.value.slice(end);
editor.value = editor.value.slice(0, start) + text + editor.value.slice(end);
} else {
document.execCommand("insertText", false, text);
document.execCommand('insertText', false, text);
}
}

// Don't bother attaching the paste event if we're on a site we don't want to run on.
let attach = true;

const blockedSites = [
"gist.github.com",
"facebook.com",
"slack.com",
"twitter.com",
"whatsapp.com",
'gist.github.com',
'facebook.com',
'slack.com',
'twitter.com',
'whatsapp.com',
];

if (blockedSites.some((site) => document.domain.includes(site))) {
if (blockedSites.some(site => document.domain.includes(site))) {
attach = false;
}

// Don't load on o2 sites, as they already have this feature.
if (document.body.classList.contains("o2")) {
if (document.body.classList.contains('o2')) {
attach = false;
}

// No need to load in Discourse, as it already has this feature.
if (document.getElementsByClassName("discourse-root").length > 0) {
if (document.querySelectorAll('.discourse-root').length > 0) {
attach = false;
}

if (attach) {
document.addEventListener("paste", pasteHandler);
document.addEventListener("keydown", shiftChecker);
document.addEventListener('paste', pasteHandler);
document.addEventListener('keydown', shiftChecker);
}

0 comments on commit 9f0ddc5

Please sign in to comment.