1
0
Fork 0
This repository has been archived on 2024-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
piwik-checker/js/background.js

79 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2017-07-06 14:35:58 +02:00
// Chrome automatically creates a background.html page for this to execute.
// This can access the inspected page via executeScript
//
// Can use:
// chrome.tabs.*
// chrome.extension.*
2017-07-08 13:01:02 +02:00
chrome.runtime.onConnect.addListener(function(port) {
2017-07-06 14:35:58 +02:00
2017-07-07 22:54:29 +02:00
function extensionListener(message, sender, sendResponse) {
if (message.tabId) {
if (message.action === 'inject') {
chrome.tabs.executeScript({
code: 'tests.main();'
}, function(result) {
port.postMessage({action: "injectResponse", data: result[0]});
});
2017-08-13 15:23:32 +02:00
console.log("inject");
}
var handleWebRequest;
2017-07-08 13:01:02 +02:00
if (message.action === "request") {
2017-07-07 22:54:29 +02:00
console.log("request");
handleWebRequest = function(details, test) {
2017-07-06 14:35:58 +02:00
console.log(details);
2017-07-07 22:54:29 +02:00
console.log(test);
2017-07-07 19:26:58 +02:00
var file;
if (details.url.indexOf("piwik.js") !== -1) {
file = "piwik.js";
} else if (details.url.indexOf("action_name") !== -1) {
file = "piwik.php";
}
2017-07-06 16:50:41 +02:00
port.postMessage({
action: "requestResponse",
2017-07-07 19:26:58 +02:00
file: file,
2017-07-06 16:50:41 +02:00
data: details
});
2017-07-07 22:54:29 +02:00
};
2017-07-06 16:50:41 +02:00
2017-07-07 22:54:29 +02:00
chrome.webRequest.onCompleted.addListener(handleWebRequest,
{urls: ["*://*/piwik.js", "*://*/piwik.php*"], tabId: message.tabId} // only look for request in open tab
);
chrome.webRequest.onErrorOccurred.addListener(handleWebRequest,
{urls: ["*://*/piwik.js", "*://*/piwik.php*"], tabId: message.tabId} // only look for request in open tab
);
}
2017-07-06 14:35:58 +02:00
2017-07-07 22:54:29 +02:00
// This accepts messages from the inspectedPage and
// sends them to the panel
} else {
port.postMessage(message);
}
sendResponse(message);
2017-07-06 14:35:58 +02:00
}
2017-07-07 22:54:29 +02:00
// Listens to messages sent from the panel
2017-07-08 13:01:02 +02:00
chrome.runtime.onMessage.addListener(extensionListener);
2017-07-07 22:54:29 +02:00
console.log("connected to extention");
2017-07-06 14:35:58 +02:00
2017-07-07 22:54:29 +02:00
port.onDisconnect.addListener(function(port) {
2017-07-08 13:01:02 +02:00
chrome.runtime.onMessage.removeListener(extensionListener);
2017-07-07 22:54:29 +02:00
});
2017-07-06 14:35:58 +02:00
2017-07-07 22:54:29 +02:00
}
);
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action === "test") {
if (message.usesPiwik) {
2017-07-08 11:15:49 +02:00
chrome.pageAction.show(sender.tab.id);
} else {
chrome.pageAction.hide(sender.tab.id);
2017-07-07 22:54:29 +02:00
}
}
2017-07-06 14:35:58 +02:00
});