Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Zepto.js instead of jQuery where possible #670

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions chromeipass/background/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ event.onMessage = function(request, sender, callback) {
sender.tab.id = page.currentTabId;
}

event.invoke(event.messageHandlers[request.action], callback, sender.tab.id, request.args);
event.invoke(event.messageHandlers[request.action], callback, sender.tab.id, sender.frameId, request.args);

// onMessage closes channel for callback automatically
// if this method does not return true
Expand All @@ -31,7 +31,7 @@ event.onMessage = function(request, sender, callback) {
* @param {bool} secondTime
* @returns null (asynchronous)
*/
event.invoke = function(handler, callback, senderTabId, args, secondTime) {
event.invoke = function(handler, callback, senderTabId, senderFrameId, args, secondTime) {
if(senderTabId < 1) {
return;
}
Expand All @@ -58,7 +58,7 @@ event.invoke = function(handler, callback, senderTabId, args, secondTime) {
// using window.open()
if (!secondTime) {
window.setTimeout(function() {
event.invoke(handler, callback, senderTabId, args, true);
event.invoke(handler, callback, senderTabId, senderFrameId, args, true);
}, 250);
}
return;
Expand All @@ -72,6 +72,7 @@ event.invoke = function(handler, callback, senderTabId, args, secondTime) {

args.unshift(tab);
args.unshift(callback);
args.push(senderFrameId);

if(handler) {
handler.apply(this, args);
Expand Down Expand Up @@ -215,13 +216,25 @@ event.onMultipleFieldsPopup = function(callback, tab) {
browserAction.show(null, tab);
}

event.onExecuteScript = function (callback, tab, filenames, frameId) {
var n = 0;
for (var i = 0; i !== filenames.length; ++i) {
chrome.tabs.executeScript(tab.id, {file: filenames[i], frameId: frameId}, function () {
++n;
if (n === filenames.length) {
callback(filenames);
}
});
}
}

// all methods named in this object have to be declared BEFORE this!
event.messageHandlers = {
'add_credentials': keepass.addCredentials,
'alert': event.onShowAlert,
'associate': keepass.associate,
'check_update_keepasshttp': event.onCheckUpdateKeePassHttp,
'execute_script': event.onExecuteScript,
'get_connected_database': event.onGetConnectedDatabase,
'get_keepasshttp_versions': event.onGetKeePassHttpVersions,
'get_settings': event.onGetSettings,
Expand Down
138 changes: 103 additions & 35 deletions chromeipass/chromeipass.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
// contains already called method names
var _called = {};
var cIPJQ_full = typeof cIPJQ !== 'undefined' ? cIPJQ : null;
var cIPJQ_lite = Zepto;
(function (zepto) { // add missing functionality
zepto.outerWidth = function() {
var el = this[0];
var v = el.offsetWidth;
var style = getComputedStyle(el, '');
v += parseInt(style.marginLeft) + parseInt(style.marginRight);
return v;
};
zepto.outerHeight = function() {
var el = this[0];
var v = el.offsetHeight;
var style = getComputedStyle(el, '');
v += parseInt(style.marginTop) + parseInt(style.marginBottom);
return v;
};
})(Object.getPrototypeOf(cIPJQ_lite()));
cIPJQ = cIPJQ_lite;

var with_cIPJQ_full = (function () {
var queue = null;
return function (func/*, args...*/) {
var args = [];
for (var i = 0; i !== arguments.length; ++i) {
args.push(arguments[i]);
}
if (cIPJQ_full === null) {
if (queue === null) {
queue = [];
chrome.runtime.sendMessage({
"action": "execute_script",
"args": [["jquery-1.11.1.min.js", "jquery-ui-1.10.2.custom.min.js"]]
}, function () {
cIPJQ_full = cIPJQ;
cIPJQ = cIPJQ_lite;
for (var i = 0; i !== queue.length; ++i) {
var callback = queue[i];
callback.func.apply(callback.this_, callback.args);
}
});
}
queue.push({this_: this, func: func, args: args});
} else {
func.apply(this, args);
}
};
})();

chrome.extension.onMessage.addListener(function(req, sender, callback) {
if ('action' in req) {
if ('action' in req) with_cIPJQ_full(function () {
if(req.action == "fill_user_pass_with_specific_login") {
if(cip.credentials[req.id]) {
var combination = null;
Expand Down Expand Up @@ -48,18 +96,16 @@ chrome.extension.onMessage.addListener(function(req, sender, callback) {
cipEvents.triggerActivatedTab();
}
else if (req.action == "redetect_fields") {
chrome.extension.sendMessage({
"action": "get_settings",
}, function(response) {
cip.settings = response.data;
cip.initCredentialFields(true);
});
cip.init(true);
}
}
});
});

function _f(fieldId) {
var field = (fieldId) ? cIPJQ("input[data-cip-id='"+fieldId+"']:first") : [];
function _f(fieldId, $) {
if (typeof $ === 'undefined') {
$ = cIPJQ;
}
var field = (fieldId) ? $("input[data-cip-id='"+fieldId+"']:first") : [];
return (field.length > 0) ? field : null;
}

Expand All @@ -77,7 +123,7 @@ cipAutocomplete.elements = [];

cipAutocomplete.init = function(field) {
if(field.hasClass("cip-ui-autocomplete-input")) {
//_f(credentialInputs[i].username).autocomplete("source", autocompleteSource);
//_f(credentialInputs[i].username, cIPJQ_full).autocomplete("source", autocompleteSource);
field.autocomplete("destroy");
}

Expand All @@ -95,7 +141,7 @@ cipAutocomplete.init = function(field) {
}

cipAutocomplete.onClick = function() {
cIPJQ(this).autocomplete("search", cIPJQ(this).val());
cIPJQ_full(this).autocomplete("search", cIPJQ(this).val());
}

cipAutocomplete.onOpen = function(event, ui) {
Expand Down Expand Up @@ -140,7 +186,7 @@ cipAutocomplete.onFocus = function() {
cip.u = cIPJQ(this);

if(cIPJQ(this).val() == "") {
cIPJQ(this).autocomplete("search", "");
cIPJQ_full(this).autocomplete("search", "");
}
}

Expand All @@ -149,18 +195,41 @@ cipAutocomplete.onFocus = function() {
var cipPassword = {};

cipPassword.observedIcons = [];
cipPassword.observingLock = false;

cipPassword.init = function() {
if("initPasswordGenerator" in _called) {
return;
}

_called.initPasswordGenerator = true;

window.setInterval(function() {
var loader = function () {
cipPassword.checkObservedElements();
}, 400);
};
(function (this_) {
var observer;
var observer_target = document.body;
var observer_callback = function (mutations) {
observer.disconnect();
try {
if (mutations) {
for (var i = 0; i !== mutations.length; ++i) {
var addedNodes = mutations[i].addedNodes;
for (var j = 0; j !== addedNodes.length; ++j) {
var node = addedNodes[j];
if (node.nodeType === Node.ELEMENT_NODE) {
loader.apply(this_);
}
}
}
}
} finally {
observer.observe(observer_target, {childList: true, subtree: true, attributes: false, characterData: false});
}
};
loader(observer_target);
observer = new MutationObserver(observer_callback);
observer_callback();
})(this);
}

cipPassword.initField = function(field, inputs, pos) {
Expand Down Expand Up @@ -198,7 +267,7 @@ cipPassword.createDialog = function() {

_called.passwordCreateDialog = true;

var $dialog = cIPJQ("<div>")
var $dialog = cIPJQ_full("<div>")
.attr("id", "cip-genpw-dialog");

var $divFloat = cIPJQ("<div>").addClass("cip-genpw-clearfix");
Expand Down Expand Up @@ -305,7 +374,7 @@ cipPassword.createDialog = function() {
$dialog.append($btnFillIn);

$dialog.hide();
cIPJQ("body").append($dialog);
cIPJQ_full("body").append($dialog);
$dialog.dialog({
closeText: "×",
autoOpen: false,
Expand All @@ -315,7 +384,7 @@ cipPassword.createDialog = function() {
title: "Password Generator",
open: function(event, ui) {
cIPJQ(".cip-ui-widget-overlay").click(function() {
cIPJQ("#cip-genpw-dialog:first").dialog("close");
cIPJQ_full("#cip-genpw-dialog:first").dialog("close");
});

if(cIPJQ("input#cip-genpw-textfield-password:first").val() == "") {
Expand Down Expand Up @@ -368,7 +437,7 @@ cipPassword.createIcon = function(field) {
return;
}

var $dialog = cIPJQ("#cip-genpw-dialog");
var $dialog = cIPJQ_full("#cip-genpw-dialog");
if($dialog.dialog("isOpen")) {
$dialog.dialog("close");
}
Expand Down Expand Up @@ -432,11 +501,6 @@ cipPassword.onRequestPassword = function() {
}

cipPassword.checkObservedElements = function() {
if(cipPassword.observingLock) {
return;
}

cipPassword.observingLock = true;
cIPJQ.each(cipPassword.observedIcons, function(index, iconField) {
if(iconField && iconField.length == 1) {
var fieldId = iconField.data("cip-genpw-field-id");
Expand All @@ -459,7 +523,6 @@ cipPassword.checkObservedElements = function() {
cipPassword.observedIcons.splice(index, 1);
}
});
cipPassword.observingLock = false;
}


Expand Down Expand Up @@ -1109,16 +1172,12 @@ cip.submitUrl = null;
// received credentials from KeePassHTTP
cip.credentials = [];

cIPJQ(function() {
cip.init();
});

cip.init = function() {
cip.init = function(force) {
chrome.extension.sendMessage({
"action": "get_settings",
}, function(response) {
cip.settings = response.data;
cip.initCredentialFields();
cip.initCredentialFields(force);
});
}

Expand All @@ -1129,6 +1188,10 @@ cip.initCredentialFields = function(forceCall) {
_called.initCredentialFields = true;

var inputs = cipFields.getAllFields();
if (!inputs || inputs.length === 0) {
return;
}
with_cIPJQ_full(function () {
cipFields.prepareVisibleFieldsWithID("select");
cip.initPasswordGenerator(inputs);

Expand All @@ -1154,6 +1217,7 @@ cip.initCredentialFields = function(forceCall) {
'args': [ cip.url, cip.submitUrl ]
}, cip.retrieveCredentialsCallback);
}
});
} // end function init

cip.initPasswordGenerator = function(inputs) {
Expand Down Expand Up @@ -1252,7 +1316,7 @@ cip.preparePageForMultipleCredentials = function(credentials) {
if(cip.settings.autoCompleteUsernames) {
for(var i = 0; i < cipFields.combinations.length; i++) {
if(_f(cipFields.combinations[i].username)) {
cipAutocomplete.init(_f(cipFields.combinations[i].username));
cipAutocomplete.init(_f(cipFields.combinations[i].username, cIPJQ_full));
}
}
}
Expand Down Expand Up @@ -1684,7 +1748,7 @@ cipEvents.clearCredentials = function() {

if(cip.settings.autoCompleteUsernames) {
for(var i = 0; i < cipFields.combinations.length; i++) {
var uField = _f(cipFields.combinations[i].username);
var uField = _f(cipFields.combinations[i].username, cIPJQ_full);
if(uField) {
if(uField.hasClass("cip-ui-autocomplete-input")) {
uField.autocomplete("destroy");
Expand All @@ -1707,3 +1771,7 @@ cipEvents.triggerActivatedTab = function() {
}, cip.retrieveCredentialsCallback);
}
}

cIPJQ(function() {
cip.init();
});
2 changes: 1 addition & 1 deletion chromeipass/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["jquery-1.11.1.min.js", "jquery-ui-1.10.2.custom.min.js", "chromeipass.js"],
"js": ["zepto.min.js", "zepto.selector.js", "chromeipass.js"],
"css": ["jquery-ui-1.10.2.custom.min.css", "bootstrap-btn.css", "chromeipass.css"],
"run_at": "document_idle",
"all_frames": true
Expand Down
2 changes: 2 additions & 0 deletions chromeipass/zepto.min.js

Large diffs are not rendered by default.

Loading