Skip to content

Commit

Permalink
Migrated to typewriterjs library for text animation.
Browse files Browse the repository at this point in the history
  • Loading branch information
pierluigizagaria committed Sep 6, 2020
1 parent fd550cc commit 52b9704
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
19 changes: 4 additions & 15 deletions js/popup.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
let texts = ["what's up?","What's up?"]
let lastWrite

function typeWriter(selector, text, callback) {
let i = 0
if (lastWrite) clearInterval(lastWrite)
lastWrite = setInterval(() => {
if (i < (text.length)) {
document.querySelector(selector).innerHTML = text.substring(0, ++i) + '<span class="text-cursor" aria-hidden="true"></span>';
} else if (typeof fnCallback == 'function') {
clearInterval(lastWrite)
callback()
}
}, 50)
}
var typewriter = new Typewriter(document.getElementById('text'));

function toggle() {
let state = document.querySelector("input").checked
Expand All @@ -21,12 +9,13 @@ function toggle() {
}

function showText(state){
typeWriter('#text', state ? texts[1] : texts[0])
new Typewriter(document.getElementById('text'), {delay: 50})
.typeString(state ? texts[1] : texts[0])
.start()
}

document.addEventListener('DOMContentLoaded', () => {
document.querySelector('input').addEventListener('click', toggle);

chrome.storage.sync.get(['enabled'], (options) => {
showText(options.enabled)
document.querySelector('input').checked = options.enabled
Expand Down
1 change: 1 addition & 0 deletions js/typewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
{
"matches": ["https://web.whatsapp.com/*"],
"run_at": "document_idle",
"js": [
"js/content.js"
]
"js": ["js/content.js"]
}
]
}
5 changes: 3 additions & 2 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Fredoka+One">
<link rel="stylesheet" href="css/popup.css">
<script src="js/popup.js"></script>
</head>
<body>
<section class="container">
<div class="text-wrap">
<h1 id="text"></h1>
<div id="text"></div>
</div>
<label class="switch">
<input type="checkbox" checked>
<span class="slider round"></span>
</label>
</section>
<script src="js/typewriter.js"></script>
<script src="js/popup.js"></script>
</body>
</html>

0 comments on commit 52b9704

Please sign in to comment.