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

big refactor, new tracking, donate option, etc #2

Merged
merged 3 commits into from
Mar 19, 2020
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
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
.DS_Store
agip-dv.zip
agip-dv-chrome.zip
Archive.zip
out.zip

js/agipdv.min.js
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ Esta extensión completa automaticamente el campo del digito verificador, tanto
See: https://chrome.google.com/webstore/detail/mcbihanjokabdgcbickiihbcehjbefkp

## TODO:

* handle https://lb.agip.gob.ar/vir/
* handle https://lb.agip.gob.ar/ConsultaPub/
22 changes: 22 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

echo "Building app.."

JS_FILES="\
js/jquery-3.4.1.min.js \
js/Utils.js \
js/ApiConnector.js \
js/dv-generator/DVPatentes.js \
js/dv-generator/DVABL.js \
js/common/CommonDVPage.js \
js/common/TrackingHelper.js \
js/pages/ConsultaPatPage.js \
js/pages/BajaPatPage.js \
js/pages/ConsultaABLPage.js \
js/pages/ConsultaImpuestoInmobiliarioPage.js \
js/pages/ConsultaVIRPage.js \
js/pages/ConsultaPubPage.js \
js/functions.js"
cat $JS_FILES > js/agipdv.min.js

echo "Build finished."
Empty file removed css/styles.css
Empty file.
41 changes: 41 additions & 0 deletions js/ApiConnector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let ApiConnector = function () {

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

let logMessage = function (method, isError, message) {
return postData(BASE_API_URL + "/log", {
method: method,
error: isError,
message: message
});
};

let postData = function (url, data) {
return makeRequest({
url: url,
method: 'POST',
headers: {
"X-Client": CLIENT,
"Content-type": "application/json; charset=utf-8"
},
body: JSON.stringify(data)
});
};

// ---

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;
});
};

// Public
return {
logMessage: logMessage,
};
};
87 changes: 39 additions & 48 deletions js/Utils.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,60 @@
var Utils = function() {
let Utils = function (apiConnector) {

var TRACKING_URL = "http://www.pablomatiasgomez.com.ar/agipdv/track.php";
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;

var RECOMMEND_ALERT_SHOWN_STORAGE_KEY = "agipdv.recommendAlertAlreadyShown";
var EXTENSION_TIMES_USED_STORAGE_KEY = "agipdv.timesUsed";
var MIN_TIMES_USED_TO_SHOW_RECOMMEND_ALERT = 10;
var timesUsed;

var incrementAndGetTimesUsed = function() {
var timesUsed = JSON.parse(localStorage.getItem(EXTENSION_TIMES_USED_STORAGE_KEY)) || 0;
let incrementAndGetTimesUsed = function () {
let timesUsed = JSON.parse(localStorage.getItem(EXTENSION_TIMES_USED_STORAGE_KEY)) || 0;
timesUsed++;
localStorage.setItem(EXTENSION_TIMES_USED_STORAGE_KEY, JSON.stringify(timesUsed));
console.log(timesUsed);
return timesUsed;
};
timesUsed = incrementAndGetTimesUsed();

var trackGeneratedDv = function(key, dv, additionalData) {
var data = key + "-" + dv;
if (additionalData) {
data += " - " + additionalData;
}
postData(location.href, data);
let donateAlertAlreadyShown = function () {
return JSON.parse(localStorage.getItem(DONATE_ALERT_SHOWN_STORAGE_KEY)) === true;
};

var postData = function(url, data) {
var getQueryStringKeyValue = function(key, value) {
return key + "=" + encodeURIComponent(value) + "&";
};

var body = "";
body += getQueryStringKeyValue("url", url);
body += getQueryStringKeyValue("data", data);

chrome.runtime.sendMessage({
method: 'POST',
action: 'xhttp',
url: TRACKING_URL,
data: body
}, function(responseText) { });
let setDonateAlertShown = function () {
localStorage.setItem(DONATE_ALERT_SHOWN_STORAGE_KEY, JSON.stringify(true));
};

var recommendAlertAlreadyShown = function() {
return JSON.parse(localStorage.getItem(RECOMMEND_ALERT_SHOWN_STORAGE_KEY)) === 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}));
}
};

var shouldShowRecommendAlert = function() {
return timesUsed >= MIN_TIMES_USED_TO_SHOW_RECOMMEND_ALERT && !recommendAlertAlreadyShown();
let trackGeneratedDv = function (key, dv, additionalData) {
let data = key + "-" + dv;
if (additionalData) {
data += " - " + additionalData;
}
return apiConnector.logMessage("trackGeneratedDv", false, data);
};

var showRecommendAlert = function() {
localStorage.setItem(RECOMMEND_ALERT_SHOWN_STORAGE_KEY, JSON.stringify(true));
postData(location.href, "recommendAlertShown");
if (confirm('Hola!\n\nEspero que disfrutes la extensión que autocompleta el digito verificador!\n\n¿Te gustaría dejarme una recomendación en el store?')) {
window.open('https://chrome.google.com/webstore/detail/agip-digito-verificador/mcbihanjokabdgcbickiihbcehjbefkp/reviews', '_blank');
postData(location.href, "recommendAlertOpen=true");
} else {
postData(location.href, "recommendAlertOpen=false");
}
let stringifyError = function (error) {
if (error instanceof Error) return error.toString() + "\n" + error.stack;
if (typeof error === 'object') return JSON.stringify(error);
return error;
};

return {
showDonateAlert: showDonateAlert,
trackGeneratedDv: trackGeneratedDv,

shouldShowRecommendAlert: shouldShowRecommendAlert,
showRecommendAlert: showRecommendAlert
stringifyError: stringifyError,
};
};
16 changes: 16 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
chrome.runtime.onMessage.addListener(function (requestInfo, sender, resolve) {
fetch(requestInfo.url, requestInfo).then(response => {
if (response.ok) {
return response.json();
} else {
return response.text().then(body => {
throw response.status + " - " + body
});
}
}).then(json => {
resolve(json);
}).catch(e => {
resolve({error: e});
});
return true;
});
29 changes: 0 additions & 29 deletions js/backgroundTracking.js

This file was deleted.

34 changes: 16 additions & 18 deletions js/common/CommonDVPage.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
var CommonDVPage = function(options) {
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.additionalTrackingFields) options.additionalTrackingFields = {};
if (!options.dvGenerator) throw "dvGenerator required";
if (!options.utils) throw "utils required";

var trackingPage = new TrackingPage({
$id: options.$id,
$dv: options.$dv,
$btnAction: options.$btnAction,
additionalTrackingFields: options.additionalTrackingFields,
utils: options.utils
});
var lastDv = null;

var bindEvents = function() {
var fn = function() {
var dv = options.dvGenerator.getDV(options.$id.val());
if (!isNaN(dv) && dv !== lastDv) {
lastDv = dv;
let bindEvents = function () {
let fn = function () {
let dv = options.dvGenerator.getDV(options.$id.val());
if (!isNaN(dv)) {
options.$dv.val(dv);
}
};
options.$id.on("keyup", fn);
options.$id.on("change", fn);
};

var checkCheckbox = function() {
let checkCheckbox = function () {
// We can support cases in which the check is not present in the page.
if (options.$chkUseDv && options.$chkUseDv.length && !options.$chkUseDv.prop("checked")) {
options.$chkUseDv.click();
}
};

// Init
(function() {
return Promise.resolve().then(() => {
checkCheckbox();
bindEvents();
})();
}).then(() => {
return TrackingHelper({
$id: options.$id,
$dv: options.$dv,
$btnAction: options.$btnAction,
additionalTrackingFields: options.additionalTrackingFields,
utils: options.utils
});
});
};

17 changes: 11 additions & 6 deletions js/common/TrackingPage.js → js/common/TrackingHelper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
var TrackingPage = function(options) {
/**
* Helepr 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.additionalTrackingFields) options.additionalTrackingFields = {};
if (!options.utils) throw "utils required";

var bindEvents = function() {
options.$btnAction.on("click", function() {
var additionalTrackingFields = {};
let bindEvents = function () {
options.$btnAction.on("click", function () {
let additionalTrackingFields = {};
Object.keys(options.additionalTrackingFields).forEach(additionalTrackingFieldKey => {
if (options.additionalTrackingFields[additionalTrackingFieldKey].length) {
additionalTrackingFields[additionalTrackingFieldKey] = options.additionalTrackingFields[additionalTrackingFieldKey].val();
Expand All @@ -22,8 +27,8 @@ var TrackingPage = function(options) {
};

// Init
(function() {
return Promise.resolve().then(() => {
bindEvents();
})();
});
};

22 changes: 10 additions & 12 deletions js/dv-generator/DVABL.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var DVABL = function() {
var multipliers = [
let DVABL = function () {
let multipliers = [
7,
2,
3,
Expand All @@ -9,22 +9,20 @@ var DVABL = function() {
7
];

var getDV = function(partida) {
var i;

let getDV = function (partida) {
partida = partida.toString().toUpperCase().replace(" ", "").replace("-", "");
if (partida.length == 6) partida = "0" + partida;
if (partida.length != 7) return NaN;
if (partida.length === 6) partida = "0" + partida;
if (partida.length !== 7) return NaN;

var sum = 0;
for (i = 0; i < 7; i++) {
let sum = 0;
for (let i = 0; i < 7; i++) {
sum += (parseInt(partida[i]) * multipliers[i]);
}

var dv = sum % 11;
let dv = sum % 11;

var dvStr;
if (dv == 10) {
let dvStr;
if (dv === 10) {
dvStr = "01";
} else if (dv < 10) {
dvStr = "0" + dv.toString();
Expand Down
Loading