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/background.js

62 lines
2.2 KiB
JavaScript
Raw 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.*
chrome.extension.onConnect.addListener(function(port) {
function extensionListener(message, sender, sendResponse) {
if (message.tabId) {
if (message.action === 'inject') {
//Evaluate script in inspectedPage
chrome.tabs.executeScript(message.tabId, {file: "inject.js"}, function(result) {
2017-07-06 16:50:41 +02:00
port.postMessage({action: "injectResponse", data: result[0]});
2017-07-06 14:35:58 +02:00
});
2017-07-06 16:50:41 +02:00
console.log("inject")
2017-07-06 14:35:58 +02:00
} else if (message.action === "request") {
2017-07-06 16:50:41 +02:00
console.log("request");
2017-07-06 14:35:58 +02:00
chrome.webRequest.onCompleted.addListener(
function(details) {
console.log(details);
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-06 14:35:58 +02:00
},
{urls: ["*://*/piwik.js", "*://*/piwik.php*"], tabId: message.tabId} // only look for request in open tab
);
}
// This accepts messages from the inspectedPage and
// sends them to the panel
} else {
port.postMessage(message);
}
sendResponse(message);
}
// Listens to messages sent from the panel
chrome.extension.onMessage.addListener(extensionListener);
console.log("connected to extention");
port.onDisconnect.addListener(function(port) {
chrome.extension.onMessage.removeListener(extensionListener);
});
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
return true;
});