1
0
Fork 0

first version

This commit is contained in:
Lukas Winkler 2017-07-06 14:35:58 +02:00
commit c9ccb241bd
8 changed files with 159 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.idea/
check.md

54
background.js Normal file
View file

@ -0,0 +1,54 @@
// 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.*
console.log("willBeSendToBackgroundJs");
chrome.extension.onConnect.addListener(function(port) {
extentionID = chrome.runtime.id;
function extensionListener(message, sender, sendResponse) {
console.log("got message");
if (message.tabId) {
if (message.action === 'inject') {
//Evaluate script in inspectedPage
chrome.tabs.executeScript(message.tabId, {file: "inject.js"}, function(result) {
port.postMessage(result);
console.log(result);
});
console.log("got message")
} else if (message.action === "request") {
chrome.webRequest.onCompleted.addListener(
function(details) {
console.log(details);
// return {cancel: details.url.indexOf("://piwik.gattinger-wachau.at") !== -1};
},
{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;
});

8
devtools.html Normal file
View file

@ -0,0 +1,8 @@
<!doctype html>
<html>
<head>
<script src="devtools.js"></script>
</head>
<body>
</body>
</html>

4
devtools.js Normal file
View file

@ -0,0 +1,4 @@
chrome.devtools.panels.create("Piwik Checker","chrome.png", "panel.html", function(panel) {
console.log("panel loaded");
});

36
inject.js Normal file
View file

@ -0,0 +1,36 @@
tests = {
errors: [],
piwikJSScriptObject: function() {
var allElements = document.getElementsByTagName('script');
for (var i = 0, n = allElements.length; i < n; i++) {
if (allElements[i].hasAttribute("src") && allElements[i].getAttribute("src").endsWith("piwik.js")) {// TODO: support renamed piwik.js
return allElements[i];
}
}
},
/**
* @return {string}
*/
URLtoPiwikJS: function() {
return this.scriptObject.getAttribute("src");
},
isScriptAsync: function() {
return this.scriptObject.hasAttribute("async") && this.scriptObject.hasAttribute("defer")
},
main: function() {
this.scriptObject = this.piwikJSScriptObject();
if (!this.scriptObject) {
console.warn("No piwik found");
return false;
}
var results = {
UrltoPiwikJs: this.URLtoPiwikJS()
};
console.log(this.URLtoPiwikJS());
console.log(this.isScriptAsync());
return results;
}
};
willBeSendToBackgroundJs = tests.main();

19
manifest.json Normal file
View file

@ -0,0 +1,19 @@
{
"manifest_version": 2,
"name": "Piwik Checker",
"description": "Description",
"version": "0.1",
"devtools_page": "devtools.html",
"permissions": [
"webRequest",
"webRequestBlocking",
"tabs",
"http://*/*",
"https://*/*"
],
"background": {
"scripts": [
"background.js"
]
}
}

11
panel.html Normal file
View file

@ -0,0 +1,11 @@
<html>
<head>
<script src="panel.js"></script>
</head>
<body>
<h2>DevTools panel</h2>
<p>Your content goes here.</p>
<button id="start">Start</button>
<pre id="response"></pre>
</body>
</html>

25
panel.js Normal file
View file

@ -0,0 +1,25 @@
document.addEventListener('DOMContentLoaded', function() {
(function createChannel() {
//Create a port with background page for continous message communication
var port = chrome.extension.connect({
name: "Sample Communication" //Given a Name
});
// Listen to messages from the background page
port.onMessage.addListener(function(message) {
document.querySelector('#response').innerHTML = JSON.stringify(message, null, 4);
// port.postMessage(message);
});
}());
console.log(document.querySelector('#start'));
document.querySelector('#start').addEventListener('click', function() {
chrome.extension.sendMessage({action: "inject", tabId: chrome.devtools.inspectedWindow.tabId});
chrome.extension.sendMessage({action: "request", tabId: chrome.devtools.inspectedWindow.tabId});
console.log("sent message")
}, false);
});