-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
88 lines (80 loc) · 3.19 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let forcePasterSettings = {
isPasteEnabled: false,
clickCount: 0,
pasteCount: 0,
};
chrome.storage.local.get(['forcepaster'], function(item) {
forcePasterSettings = item.forcepaster || forcePasterSettings;
})
chrome.storage.onChanged.addListener(function(changes) {
forcePasterSettings = changes.forcepaster.newValue || forcePasterSettings;
});
let darkModeListener = (isDarkMode) => {
chrome.runtime.sendMessage({
type: "themechange",
mode: isDarkMode.matches ? 'dark' : 'light',
});
}
// MediaQueryList
const darkModePreference = window.matchMedia("(prefers-color-scheme: dark)");
// recommended method for newer browsers: specify event-type as first argument
darkModePreference.addEventListener("change", darkModeListener);
// deprecated method for backward compatibility
darkModePreference.addListener(e => darkModeListener);
// set icons on initial load
darkModeListener(darkModePreference);
const isInputOrTextarea = (currEle) => ["input", "textarea"].includes(currEle.tagName.toLowerCase());
document.body.onpaste = event => {
let currEle = document.activeElement;
if (forcePasterSettings.isPasteEnabled) {
chrome.runtime.sendMessage({ type: "onpastestart", on: currEle.tagName.toLowerCase() })
if (!isInputOrTextarea(currEle)) return;
let currVal = currEle.value;
let finalVal = "";
// Stop data actually being pasted into div
event.stopPropagation();
event.preventDefault();
// Get pasted data via clipboard API
let clipboardData = event.clipboardData || window.clipboardData || event.originalEvent.clipboardData;
const pastedText = clipboardData.getData('Text');
finalVal = currVal.slice(0, currEle.selectionStart) + pastedText;
let caretPos = finalVal.length; //get position to place caret after pasting
finalVal += currVal.slice(currEle.selectionEnd);
currEle.value = "";
currEle.value = finalVal;
setCaretPositionToEndOfPastedText(currEle, caretPos);
chrome.runtime.sendMessage({ type: "onpastecomplete" }, response => {
if (response.totalPastes > 10) {
// TODO: show a dismissible box at the top right of the page
// asking users to rate the extension on the webstore if they liked using it
// show buttons to proceed, rate never, or rate later.
}
});
}
};
function setCaretPositionToEndOfPastedText(elem, caretPos) {
if(elem != null) {
if(elem.createTextRange) {
let range = elem.createTextRange();
range.move('character', caretPos);
range.select();
} else {
if (elem.selectionStart) {
elem.focus();
elem.setSelectionRange(caretPos, caretPos);
} else {
elem.focus();
}
}
}
}
document.body.onkeydown = async event => {
if (forcePasterSettings.isPasteEnabled
&& window.location.hostname == "www.pw.live"
&& event.target.tagName.toLowerCase() === 'input'
) {
if ((event.ctrlKey || event.metaKey) && event.key === 'v') {
document.execCommand('paste')
}
}
};