re-inject extension on load

This commit is contained in:
Florian Dold 2016-03-02 15:33:23 +01:00
parent 18904a4a6d
commit 13a8f36cad
4 changed files with 71 additions and 25 deletions

View File

@ -34,14 +34,29 @@ var TalerNotify;
url = url.replace("${$}", "$"); url = url.replace("${$}", "$");
return url; return url;
} }
var $ = function (x) { return document.getElementById(x); }; var handlers = [];
document.addEventListener("DOMContentLoaded", function (e) { var port = chrome.runtime.connect();
if (document.documentElement.getAttribute("data-taler-requested")) { port.onDisconnect.addListener(function () {
console.log("taler requested in html element"); console.log("chrome runtime disconnected");
document.documentElement.setAttribute("data-taler-extension-id", chrome.runtime.id); for (var _i = 0, handlers_1 = handlers; _i < handlers_1.length; _i++) {
var handler = handlers_1[_i];
document.removeEventListener(handler.type, handler.listener);
} }
}); });
document.addEventListener("taler-probe", function (e) { var $ = function (x) { return document.getElementById(x); };
function addHandler(type, listener) {
document.addEventListener(type, listener);
handlers.push({ type: type, listener: listener });
}
addHandler("taler-query-id", function (e) {
var evt = new CustomEvent("taler-id", {
detail: {
id: chrome.runtime.id
}
});
document.dispatchEvent(evt);
});
addHandler("taler-probe", function (e) {
var evt = new CustomEvent("taler-wallet-present", { var evt = new CustomEvent("taler-wallet-present", {
detail: { detail: {
walletProtocolVersion: PROTOCOL_VERSION walletProtocolVersion: PROTOCOL_VERSION
@ -50,7 +65,7 @@ var TalerNotify;
document.dispatchEvent(evt); document.dispatchEvent(evt);
console.log("handshake done"); console.log("handshake done");
}); });
document.addEventListener("taler-create-reserve", function (e) { addHandler("taler-create-reserve", function (e) {
console.log("taler-create-reserve with " + JSON.stringify(e.detail)); console.log("taler-create-reserve with " + JSON.stringify(e.detail));
var params = { var params = {
amount: JSON.stringify(e.detail.amount), amount: JSON.stringify(e.detail.amount),
@ -61,7 +76,7 @@ var TalerNotify;
var uri = URI(chrome.extension.getURL("pages/confirm-create-reserve.html")); var uri = URI(chrome.extension.getURL("pages/confirm-create-reserve.html"));
document.location.href = uri.query(params).href(); document.location.href = uri.query(params).href();
}); });
document.addEventListener("taler-confirm-reserve", function (e) { addHandler("taler-confirm-reserve", function (e) {
console.log("taler-confirm-reserve with " + JSON.stringify(e.detail)); console.log("taler-confirm-reserve with " + JSON.stringify(e.detail));
var msg = { var msg = {
type: "confirm-reserve", type: "confirm-reserve",
@ -74,7 +89,7 @@ var TalerNotify;
}); });
}); });
// XXX: remove in a bit, just here for compatibility ... // XXX: remove in a bit, just here for compatibility ...
document.addEventListener("taler-contract", function (e) { addHandler("taler-contract", function (e) {
// XXX: the merchant should just give us the parsed data ... // XXX: the merchant should just give us the parsed data ...
var offer = JSON.parse(e.detail); var offer = JSON.parse(e.detail);
if (!offer.contract) { if (!offer.contract) {
@ -108,7 +123,7 @@ var TalerNotify;
} }
}); });
}); });
document.addEventListener("taler-confirm-contract", function (e) { addHandler("taler-confirm-contract", function (e) {
if (!e.detail.contract_wrapper) { if (!e.detail.contract_wrapper) {
console.error("contract wrapper missing"); console.error("contract wrapper missing");
return; return;
@ -151,7 +166,7 @@ var TalerNotify;
} }
}); });
}); });
document.addEventListener('taler-execute-payment', function (e) { addHandler('taler-execute-payment', function (e) {
console.log("got taler-execute-payment in content page"); console.log("got taler-execute-payment in content page");
if (!e.detail.pay_url) { if (!e.detail.pay_url) {
console.log("field 'pay_url' missing in taler-execute-payment event"); console.log("field 'pay_url' missing in taler-execute-payment event");

View File

@ -43,17 +43,34 @@ namespace TalerNotify {
return url; return url;
} }
let $ = (x) => document.getElementById(x); let handlers = [];
document.addEventListener("DOMContentLoaded", function(e) { let port = chrome.runtime.connect();
if (document.documentElement.getAttribute("data-taler-requested")) { port.onDisconnect.addListener(() => {
console.log("taler requested in html element"); console.log("chrome runtime disconnected");
document.documentElement.setAttribute("data-taler-extension-id", for (let handler of handlers) {
chrome.runtime.id); document.removeEventListener(handler.type, handler.listener);
} }
}); });
document.addEventListener("taler-probe", function(e) { let $ = (x) => document.getElementById(x);
function addHandler(type, listener) {
document.addEventListener(type, listener);
handlers.push({type, listener});
}
addHandler("taler-query-id", function(e) {
let evt = new CustomEvent("taler-id", {
detail: {
id: chrome.runtime.id
}
});
document.dispatchEvent(evt);
});
addHandler("taler-probe", function(e) {
let evt = new CustomEvent("taler-wallet-present", { let evt = new CustomEvent("taler-wallet-present", {
detail: { detail: {
walletProtocolVersion: PROTOCOL_VERSION walletProtocolVersion: PROTOCOL_VERSION
@ -63,7 +80,7 @@ namespace TalerNotify {
console.log("handshake done"); console.log("handshake done");
}); });
document.addEventListener("taler-create-reserve", function(e: CustomEvent) { addHandler("taler-create-reserve", function(e: CustomEvent) {
console.log("taler-create-reserve with " + JSON.stringify(e.detail)); console.log("taler-create-reserve with " + JSON.stringify(e.detail));
let params = { let params = {
amount: JSON.stringify(e.detail.amount), amount: JSON.stringify(e.detail.amount),
@ -75,7 +92,7 @@ namespace TalerNotify {
document.location.href = uri.query(params).href(); document.location.href = uri.query(params).href();
}); });
document.addEventListener("taler-confirm-reserve", function(e: CustomEvent) { addHandler("taler-confirm-reserve", function(e: CustomEvent) {
console.log("taler-confirm-reserve with " + JSON.stringify(e.detail)); console.log("taler-confirm-reserve with " + JSON.stringify(e.detail));
let msg = { let msg = {
type: "confirm-reserve", type: "confirm-reserve",
@ -90,7 +107,7 @@ namespace TalerNotify {
// XXX: remove in a bit, just here for compatibility ... // XXX: remove in a bit, just here for compatibility ...
document.addEventListener("taler-contract", function(e: CustomEvent) { addHandler("taler-contract", function(e: CustomEvent) {
// XXX: the merchant should just give us the parsed data ... // XXX: the merchant should just give us the parsed data ...
let offer = JSON.parse(e.detail); let offer = JSON.parse(e.detail);
@ -130,7 +147,7 @@ namespace TalerNotify {
}); });
document.addEventListener("taler-confirm-contract", function(e: CustomEvent) { addHandler("taler-confirm-contract", function(e: CustomEvent) {
if (!e.detail.contract_wrapper) { if (!e.detail.contract_wrapper) {
console.error("contract wrapper missing"); console.error("contract wrapper missing");
return; return;
@ -179,7 +196,7 @@ namespace TalerNotify {
}); });
document.addEventListener('taler-execute-payment', function(e: CustomEvent) { addHandler('taler-execute-payment', function(e: CustomEvent) {
console.log("got taler-execute-payment in content page"); console.log("got taler-execute-payment in content page");
if (!e.detail.pay_url) { if (!e.detail.pay_url) {
console.log("field 'pay_url' missing in taler-execute-payment event"); console.log("field 'pay_url' missing in taler-execute-payment event");
@ -232,4 +249,4 @@ namespace TalerNotify {
}; };
}); });
}); });
} }

View File

@ -195,6 +195,19 @@ class ChromeNotifier implements Notifier {
export function wxMain() { export function wxMain() {
chrome.browserAction.setBadgeText({text: ""}); chrome.browserAction.setBadgeText({text: ""});
chrome.tabs.query({}, function(tabs) {
for (let tab of tabs) {
if (!tab.url) {
return;
}
let uri = URI(tab.url);
if (uri.protocol() == "http" || uri.protocol() == "https") {
console.log("injecting into existing tab", tab.id);
chrome.tabs.executeScript(tab.id, {file: "content_scripts/notify.js"});
}
}
});
Promise.resolve() Promise.resolve()
.then(() => { .then(() => {
return openTalerDb(); return openTalerDb();

View File

@ -2,7 +2,7 @@
"description": "Privacy preserving and transparent payments", "description": "Privacy preserving and transparent payments",
"manifest_version": 2, "manifest_version": 2,
"name": "GNU Taler Wallet (git)", "name": "GNU Taler Wallet (git)",
"version": "0.5.14", "version": "0.5.15",
"applications": { "applications": {
"gecko": { "gecko": {
@ -12,6 +12,7 @@
"permissions": [ "permissions": [
"storage", "storage",
"tabs",
"http://*/*", "http://*/*",
"https://*/*" "https://*/*"
], ],