Skip to content

Commit

Permalink
Merge pull request #5 from jamesmortensen/manifest-v3
Browse files Browse the repository at this point in the history
Upgrade to Manifest v3 and Implement Quote Insertion in Replies
  • Loading branch information
jamesmortensen authored Sep 24, 2022
2 parents 6b4bcb6 + cc28107 commit 05b5285
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 171 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

----

Copyright 2013, 2014, James Mortensen
Copyright 2013, 2014, 2022, James Mortensen

I made this extension because I couldn't find a tool to let me randomly inject my own user-selected
quotes into my email signature. I specifically wanted the quotes to be hand-selected.
Expand Down Expand Up @@ -33,3 +33,7 @@
// @history 0.9.3 Modified the quote manager to make it easier to use.

// @history 1.0.0 Deployed 1.0.0 to the Chrome Web Store.

// @history 2.0.0 Upgrade to manifest v3. Added ability to have quotes with replies.

// @history 2.0.1 Fixed bug where only the 29th quote in the list is inserted.
3 changes: 3 additions & 0 deletions chrome-extension/css/quoteManagerPageAction.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ textarea {
left: 4%;
margin-bottom: 15px;
}
.modal-footer span.alert {
padding: 10px 15px 11px 15px;
}
.icon {
margin-right: 52px;
}
Expand Down
48 changes: 0 additions & 48 deletions chrome-extension/js/background.js

This file was deleted.

73 changes: 73 additions & 0 deletions chrome-extension/js/lib/fade-in-out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// fade-in-out.js - https://github.com/jamesmortensen/fade-in-out-visibility @d6c9febb630e43e7bb3ac3997198fd8968cec139 - Sat Sep 17 14:26:23 2022 +0530

/*
Similar to jQuery's fadeIn and fadeOut, except relies on visibility and opacity instead of display,
which preserves element height and width.
*/

class ElementFader {

constructor(selector, doc) {
doc = doc || window.document;
this.selector = selector;
this.elem = doc.querySelector(selector);
this.opacity = parseFloat(this.elem.style.opacity);
this.queue = [];
this.interval = null;
}

fadeIn() {
if(this.queue.length === 4)
this.queue.shift();
this.queue.push(this.#fadeIn);
if (this.interval === null) {
this.queue.shift()(this);
}
}

fadeOut() {
if(this.queue.length === 4)
this.queue.shift();
this.queue.push(this.#fadeOut);
if (this.interval === null) {
this.queue.shift()(this);
}
}

#fadeIn(that) {
that.interval = setInterval(((_this) => {
return function () {
_this.opacity = Math.round( (_this.opacity + .1) * 10 ) / 10;
_this.elem.style.opacity = _this.opacity;
if (_this.opacity >= 1) {
clearInterval(_this.interval);
_this.interval = null;
if (_this.queue.length > 0)
_this.queue.shift()(_this);
}
}
})(that), 80); // increased to 80 from 100
}

#fadeOut(that) {
that.interval = setInterval(((_this) => {
return function () {
_this.opacity = Math.round( (_this.opacity - .1) * 10 ) / 10;
_this.elem.style.opacity = _this.opacity;
if (_this.opacity <= 0) {
clearInterval(_this.interval);
_this.interval = null;
if (_this.queue.length > 0)
_this.queue.shift()(_this);
}
}
})(that), 80); // increased to 80 from 100
}

getOpacity() {
return this.opacity;
}
}

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
module.exports = ElementFader;
4 changes: 0 additions & 4 deletions chrome-extension/js/lib/jquery-2.1.0.min.js

This file was deleted.

1 change: 0 additions & 1 deletion chrome-extension/js/lib/jquery-2.1.0.min.map

This file was deleted.

2 changes: 2 additions & 0 deletions chrome-extension/js/lib/jquery-3.6.1.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions chrome-extension/js/lib/jquery-3.6.1.min.map

Large diffs are not rendered by default.

70 changes: 22 additions & 48 deletions chrome-extension/js/quoteManagerPageAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
The MIT License (MIT)
Copyright (c) 2013, 2014 James Mortensen
Copyright (c) 2013, 2014, 2022 James Mortensen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,6 +41,7 @@ var keyFilterer;
if (typeof KeyFilterer !== 'undefined')
keyFilterer = new KeyFilterer();

var elementFader;

function getListQuoteTemplate() {
return '<li class="list-group-item quote"><input /><span class="remove-quote glyphicon glyphicon-minus"></span></li>';
Expand Down Expand Up @@ -76,36 +77,17 @@ function updateQuotePreview(targetElem) {


function storeQuotes(quotesToStore, doClose) {
chrome.storage.local.set(quotesToStore, function() {
// chrome.tabs.getCurrent(function(tab) {
// var tabId = tab.id;
// var injectDetails = {
// code: 'm_quotes = ' + quotesToStore.m_quotes
// };
// chrome.tabs.executeScript(tabId, injectDetails, function(results) {
// console.log(results);
// });

// });
chrome.storage.local.set(quotesToStore, function () {

/**
* Smoothes out the success alert message fadeIn/fadeOut.
*/
clearTimeout(g_saveTimeout);
g_saveTimeout = setTimeout(function() {
$('.modal-footer span.auto-save').fadeOut();
g_saveTimeout = setTimeout(function () {
//$('.modal-footer span.auto-save').fadeOut();
elementFader.fadeOut();
}, 250);

/**
* Inject updated quotes in the current tab only.
*/
chrome.tabs.executeScript(null,
{
code: 'randomQuoteModule.quotes = ' + JSON.stringify(quotesToStore.m_quotes)
}
);
//console.debug(quotesToStore.m_quotes[quotesToStore.m_quotes.length-1]);

if (doClose)
window.close();
});
Expand All @@ -114,7 +96,8 @@ function storeQuotes(quotesToStore, doClose) {

function showAutoSaveMessage() {
if ($('.alert-danger:visible').length === 0) {
$('.modal-footer span.auto-save').fadeIn();
//$('.modal-footer span.auto-save').fadeIn();
elementFader.fadeIn();
}
}

Expand Down Expand Up @@ -175,68 +158,59 @@ if (typeof chrome.storage === 'undefined')
var chrome = {
storage: {
local: {
get: function(_null, callback) {
get: function (_null, callback) {
var script = document.createElement('script');
script.setAttribute('src', '/quotes.js');
script.addEventListener('load', function() {
script.addEventListener('load', function () {
callback(m_quotes);
});
document.head.appendChild(script);
},
set: function() {}
set: function () { }
}
}
};



window.addEventListener("load", function() {
window.addEventListener("load", function () {

elementFader = new ElementFader('.modal-footer span.auto-save');

chrome.storage.local.get(null, loadQuotes);

/**
* This is the save action for the JSON panel, which we keep simply for backwards compatibility.
*/
$('[data-action="save"]').click(function() {
$('[data-action="save"]').click(function () {
var items = {};
if ($('[data-value="quotes"]').val().match(/\[\".*/g) != null) {
items["m_quotes"] = JSON.parse($('[data-value="quotes"]').val());
} else {
items["m_quotes"] = [$('[data-value="quotes"]').val()];
}
chrome.storage.local.set(items, function() {
chrome.storage.local.set(items, function () {
window.close();
});
//chrome.tabs.executeScript(null,{code:"document.body.dataset['quote'] = 'fasfdtest';document.body.style.backgroundColor='orange';"});
});

/**
* Close the panel.
*/
$('[data-action="cancel"]').click(function() {
$('[data-action="cancel"]').click(function () {
window.close();
});

/**
* Show the "old" JSON panel most people don't like, and be able to switch back.
*/
$('[data-action="json-panel"]').click(function() {
$('[data-action="json-panel"]').click(function () {
window.location.href = '/quoteManagerPageAction.html';
});
$('[data-action="list-panel"]').click(function() {
$('[data-action="list-panel"]').click(function () {
window.location.href = '/quoteManagerPageActionSimple.html';
});

/**
* This is the save operation for the new UX friendly list panel.
*
* @deprecated
*/
$('[data-action="save-list"]').click(function() {
var doClose = true;
saveQuotes(doClose);
});

/**
* Add a textbox and focus it.
*/
Expand All @@ -245,7 +219,7 @@ window.addEventListener("load", function() {
/**
* On every keystroke, or on cut/paste, update the quote preview.
*/
$('.list-group').on('keyup cut paste', '.quote input', function(event) {
$('.list-group').on('keyup cut paste', '.quote input', function (event) {
updateQuotePreview(event.target);
var doClose = false;
if (event.type === 'keyup') {
Expand All @@ -255,7 +229,7 @@ window.addEventListener("load", function() {
saveQuotes(doClose);
}
} else if (event.type === 'cut' || event.type === 'paste') {
setTimeout(function() {
setTimeout(function () {
saveQuotes(doClose);
}, 0);
}
Expand All @@ -274,7 +248,7 @@ window.addEventListener("load", function() {
/**
* Uncheck items marked for deletion.
*/
$('[data-action="keep-quote"]').click(function() {
$('[data-action="keep-quote"]').click(function () {
$('.list-group .quote.alert-danger').removeClass('alert').removeClass('alert-danger');
$(this).parent().fadeOut();
});
Expand Down
Loading

0 comments on commit 05b5285

Please sign in to comment.