Don't use JQuery in content script

This commit is contained in:
Alex Yatskov 2016-03-29 22:32:40 -07:00
parent 93eaed39c1
commit 6d1fc8a20e
6 changed files with 18 additions and 17 deletions

View File

@ -1,8 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<body>
<script src="../lib/handlebars.min.js"></script>
<script src="../lib/jquery-2.2.2.min.js"></script>
<script src="lib/handlebars.min.js"></script>
<script src="lib/jquery-2.2.2.min.js"></script>
<script src="templates.js"></script>
<script src="dictionary.js"></script>
<script src="deinflector.js"></script>

View File

@ -19,12 +19,15 @@
class Client {
constructor() {
this.popup = $('<div class="yomichan-popup"/>');
this.popupOffset = 10;
this.lastMosePos = null;
this.enabled = false;
$('body').append(this.popup);
this.popup = document.createElement('div');
this.popup.classList.add('yomichan-popup');
this.popup.addEventListener('mousedown', (e) => e.stopPropagation());
this.popup.addEventListener('scroll', (e) => e.stopPropagation());
document.body.appendChild(this.popup);
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
window.addEventListener('mousedown', this.onMouseDown.bind(this));
@ -32,8 +35,6 @@ class Client {
window.addEventListener('keydown', this.onKeyDown.bind(this));
window.addEventListener('scroll', (e) => this.hidePopup());
window.addEventListener('resize', (e) => this.hidePopup());
this.popup.mousedown((e) => e.stopPropagation());
this.popup.scroll((e) => e.stopPropagation());
getState((state) => this.setEnabled(state === 'enabled'));
}
@ -84,7 +85,7 @@ class Client {
} else {
range.setEnd(range.endContainer, range.startOffset + length);
renderTemplate({defs: results}, 'defs.html', (html) => {
this.popup.html(html);
this.popup.innerHTML = html;
this.showPopup(range);
});
}
@ -97,18 +98,18 @@ class Client {
selection.addRange(range);
const pos = getPopupPositionForRange(this.popup, range, this.popupOffset);
this.popup.css({left: pos.x, top: pos.y, visibility: 'visible'});
this.popup.style.left = pos.x + 'px';
this.popup.style.top = pos.y + 'px';
this.popup.style.visibility = 'visible';
}
hidePopup() {
if (this.popup.css('visibility') === 'hidden') {
return;
}
if (this.popup.style.visibility !== 'hidden') {
const selection = window.getSelection();
selection.removeAllRanges();
this.popup.css({visibility: 'hidden'});
this.popup.style.visibility = 'hidden';
}
}
setEnabled(enabled) {

View File

@ -10,7 +10,7 @@
"content_scripts": [{
"matches": ["*://*/*"],
"js": ["lib/jquery-2.2.2.min.js", "api.js", "util.js", "client.js"],
"js": ["api.js", "util.js", "client.js"],
"css": ["client.css"]
}]
}

View File

@ -54,7 +54,7 @@ function getRangePaddedRect(range) {
function getPopupPositionForRange(popup, range, offset) {
const rangeRect = range.getBoundingClientRect();
const popupRect = popup.get(0).getBoundingClientRect();
const popupRect = popup.getBoundingClientRect();
let posX = rangeRect.left;
if (posX + popupRect.width >= window.innerWidth) {