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

Develop #3

Merged
merged 1 commit into from
Aug 12, 2021
Merged
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
Esta extensión completa automaticamente el campo del digito verificador, tanto para patentes de autos, como para el ABL, etc.

See: https://chrome.google.com/webstore/detail/mcbihanjokabdgcbickiihbcehjbefkp

## TODO:
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
echo "Building app.."

JS_FILES="\
js/jquery-3.4.1.min.js \
js/jquery-3.6.0.min.js \
js/Utils.js \
js/ApiConnector.js \
js/dv-generator/DVPatentes.js \
Expand All @@ -16,7 +16,7 @@ js/pages/ConsultaABLPage.js \
js/pages/ConsultaImpuestoInmobiliarioPage.js \
js/pages/ConsultaVIRPage.js \
js/pages/ConsultaPubPage.js \
js/functions.js"
js/main.js"
cat $JS_FILES > js/agipdv.min.js

echo "Build finished."
Binary file removed images/icons/icon32.png
Binary file not shown.
9 changes: 3 additions & 6 deletions js/ApiConnector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let ApiConnector = function () {

const CLIENT = "CHROME@" + chrome.runtime.getManifest().version;
const BASE_API_URL = "http://www.pablomatiasgomez.com.ar/agipdv/v1";
const CLIENT = `CHROME@${chrome.runtime.getManifest().version}`;
const BASE_API_URL = "https://www.pablomatiasgomez.com.ar/agipdv/v1";

let logMessage = function (method, isError, message) {
return postData(BASE_API_URL + "/log", {
Expand All @@ -27,10 +27,7 @@ let ApiConnector = function () {

let makeRequest = function (options) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(options, response => (response && response.error) ? reject(response.error) : resolve(response));
}).catch(e => {
console.error("Error while making request", e);
throw e;
chrome.runtime.sendMessage(options, response => (response && response.errorStr) ? reject(new Error(response.errorStr)) : resolve(response));
});
};

Expand Down
51 changes: 16 additions & 35 deletions js/Utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
let Utils = function (apiConnector) {

const EXTENSION_TIMES_USED_STORAGE_KEY = "AgipDv.TimesUsed";
const DONATE_ALERT_SHOWN_STORAGE_KEY = "AgipDv.DonateAlertShown";
const MIN_TIMES_USED_TO_SHOW_DONATE_ALERT = 10;

let incrementAndGetTimesUsed = function () {
let timesUsed = JSON.parse(localStorage.getItem(EXTENSION_TIMES_USED_STORAGE_KEY)) || 0;
Expand All @@ -11,49 +9,32 @@ let Utils = function (apiConnector) {
return timesUsed;
};

let donateAlertAlreadyShown = function () {
return JSON.parse(localStorage.getItem(DONATE_ALERT_SHOWN_STORAGE_KEY)) === true;
};

let setDonateAlertShown = function () {
localStorage.setItem(DONATE_ALERT_SHOWN_STORAGE_KEY, JSON.stringify(true));
};

/**
* An alert will be shown only if:
* - the times this method was called is more than {@link MIN_TIMES_USED_TO_SHOW_DONATE_ALERT}
* - the alert was not shown before.
*/
let showDonateAlert = function () {
let timesUsed = incrementAndGetTimesUsed();
if (timesUsed < MIN_TIMES_USED_TO_SHOW_DONATE_ALERT || donateAlertAlreadyShown()) return;

setDonateAlertShown();
apiConnector.logMessage("donateAlertShown", false, JSON.stringify({url: window.location.href}));
if (confirm(`Hola!\n\nEspero que disfrutes la extensión que autocompleta el digito verificador!\n\n¿Te gustaría dejarme una donación? No hay minimo!`)) {
window.open('https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=7Z8VU5GP8BLY2&source=url', '_blank');
return apiConnector.logMessage("donateAlertExit", false, JSON.stringify({opened: true}));
} else {
return apiConnector.logMessage("donateAlertExit", false, JSON.stringify({opened: false}));
}
};

let trackGeneratedDv = function (key, dv, additionalData) {
let data = key + "-" + dv;
let timesUsed = incrementAndGetTimesUsed();
let message = `[Path:${window.location.pathname.toLowerCase()}] [TimesUsed:${timesUsed}] [${key}-${dv}]`;
if (additionalData) {
data += " - " + additionalData;
message += " - " + additionalData;
}
return apiConnector.logMessage("trackGeneratedDv", false, data);
return apiConnector.logMessage("trackGeneratedDv", false, message);
};

let stringifyError = function (error) {
if (error instanceof Error) return error.toString() + "\n" + error.stack;
if (typeof error === 'object') return JSON.stringify(error);
if (error instanceof Error) {
// Stack can include the message in some errors, but not in all cases.
let message = error.toString();
if (error.stack.startsWith(message)) {
return error.stack;
} else {
return message + "\n" + error.stack;
}
}
if (typeof error === "object") {
return JSON.stringify(error);
}
return error;
};

return {
showDonateAlert: showDonateAlert,
trackGeneratedDv: trackGeneratedDv,
stringifyError: stringifyError,
};
Expand Down
4 changes: 2 additions & 2 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ chrome.runtime.onMessage.addListener(function (requestInfo, sender, resolve) {
return response.json();
} else {
return response.text().then(body => {
throw response.status + " - " + body
throw new Error(`Error executing ${requestInfo.method} ${requestInfo.url} - ResponseStatus: ${response.status} - ResponseBody: ${body}`);
});
}
}).then(json => {
resolve(json);
}).catch(e => {
resolve({error: e});
resolve({errorStr: e.toString()}); // Need to do .toString() as Error is not "JSON-ifiable" and may get erased.
});
return true;
});
10 changes: 5 additions & 5 deletions js/common/CommonDVPage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let CommonDVPage = function (options) {
if (!options.$id.length) throw "$id required";
if (!options.$dv.length) throw "$dv required";
if (!options.$btnAction) throw "$btnAction required";
if (!options.$id.length) throw new Error("$id required");
if (!options.$dv.length) throw new Error("$dv required");
if (!options.$btnAction) throw new Error("$btnAction required");
if (!options.additionalTrackingFields) options.additionalTrackingFields = {};
if (!options.dvGenerator) throw "dvGenerator required";
if (!options.utils) throw "utils required";
if (!options.dvGenerator) throw new Error("dvGenerator required");
if (!options.utils) throw new Error("utils required");

let bindEvents = function () {
let fn = function () {
Expand Down
10 changes: 5 additions & 5 deletions js/common/TrackingHelper.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* Helepr to track when the user submits the form in any page.
* Helper to track when the user submits the form in any page.
* This tracks the id with the dv and other fields that can be provided
* @return {Promise<void>}
*/
let TrackingHelper = function (options) {
if (!options.$id.length) throw "$id required";
if (!options.$dv.length) throw "$dv required";
if (!options.$btnAction) throw "$btnAction required";
if (!options.$id.length) throw new Error("$id required");
if (!options.$dv.length) throw new Error("$dv required");
if (!options.$btnAction) throw new Error("$btnAction required");
if (!options.additionalTrackingFields) options.additionalTrackingFields = {};
if (!options.utils) throw "utils required";
if (!options.utils) throw new Error("utils required");

let bindEvents = function () {
options.$btnAction.on("click", function () {
Expand Down
2 changes: 0 additions & 2 deletions js/jquery-3.4.1.min.js

This file was deleted.

2 changes: 2 additions & 0 deletions js/jquery-3.6.0.min.js

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions js/functions.js → js/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(function () {
let apiConnector = new ApiConnector();
let utils = new Utils(apiConnector);
let handler = null;

const PAGE_HANDLERS = {
"/BajaPat": () => BajaPatPage(utils),
Expand All @@ -12,15 +11,12 @@
"/ConsultaPub": () => ConsultaPubPage(utils),
};

Object.entries(PAGE_HANDLERS).forEach(entry => {
if (!handler && window.location.pathname.toLowerCase().startsWith(entry[0].toLowerCase())) {
handler = entry[1];
}
});
let handler = Object.keys(PAGE_HANDLERS)
.filter(key => window.location.pathname.toLowerCase().startsWith(key.toLowerCase()))
.map(key => PAGE_HANDLERS[key])
[0];

handler && handler().then(() => {
return utils.showDonateAlert();
}).catch(e => {
handler && handler().catch(e => {
console.error("Error when handling page " + window.location.href, e);
return apiConnector.logMessage("Handle page " + window.location.pathname, true, utils.stringifyError(e));
});
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "Agip Digito Verificador",
"short_name": "AgipDv",
"version": "1.4.0",
"version": "2.0.0",

"description": "Esta extensión completa automaticamente el campo del digito verificador, tanto para patentes de autos, como para el ABL, etc.",
"author": "Pablo Matías Gomez",
Expand Down
9 changes: 5 additions & 4 deletions package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
echo "Starting packaging.."

minifyJs () {
# npm package "babel-minify" needs to be installed globally.
# npm package "minify" needs to be installed globally.
# sudo npm i minify -g
echo "Minifying $1"
mv "$1" "$1.bk"
minify "$1.bk" --out-file "$1" --builtIns false
mv "$1" "$1.bk.js"
minify "$1.bk.js" > "$1"
}
restoreJs () {
echo "Restoring $1"
rm "$1"
mv "$1.bk" "$1"
mv "$1.bk.js" "$1"
}

rm out.zip
Expand Down