reorganization
This commit is contained in:
parent
58750459d4
commit
3dbd3c0dde
5 changed files with 89 additions and 39 deletions
|
@ -7,19 +7,21 @@
|
|||
|
||||
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) {
|
||||
port.postMessage({action: "injectResponse", data: result[0]});
|
||||
});
|
||||
console.log("inject")
|
||||
} else if (message.action === "request") {
|
||||
console.log("request");
|
||||
chrome.webRequest.onCompleted.addListener(
|
||||
function(details) {
|
||||
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]});
|
||||
});
|
||||
|
||||
console.log("inject")
|
||||
} else if (message.action === "request") {
|
||||
console.log("request");
|
||||
handleWebRequest = function(details, test) {
|
||||
console.log(details);
|
||||
console.log(test);
|
||||
var file;
|
||||
if (details.url.indexOf("piwik.js") !== -1) {
|
||||
file = "piwik.js";
|
||||
|
@ -31,31 +33,43 @@ chrome.extension.onConnect.addListener(function(port) {
|
|||
file: file,
|
||||
data: details
|
||||
});
|
||||
};
|
||||
|
||||
},
|
||||
{urls: ["*://*/piwik.js", "*://*/piwik.php*"], tabId: message.tabId} // only look for request in open tab
|
||||
);
|
||||
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
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// This accepts messages from the inspectedPage and
|
||||
// sends them to the panel
|
||||
} else {
|
||||
port.postMessage(message);
|
||||
}
|
||||
|
||||
// This accepts messages from the inspectedPage and
|
||||
// sends them to the panel
|
||||
} else {
|
||||
port.postMessage(message);
|
||||
sendResponse(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);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
// 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;
|
||||
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
|
||||
console.log(sender.tab);
|
||||
if (message.action === "test") {
|
||||
if (message.usesPiwik) {
|
||||
chrome.browserAction.setBadgeText({text: "Piwik!", tabId: sender.tab.id});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
var port = chrome.runtime.connect({
|
||||
name: "devtool-comunication" //Given a Name
|
||||
});
|
||||
|
||||
chrome.devtools.panels.create("Piwik Checker","chrome.png", "panel.html", function(panel) {
|
||||
chrome.devtools.panels.create("Piwik Checker", "chrome.png", "panel.html", function(panel) {
|
||||
console.log("panel loaded");
|
||||
});
|
||||
|
|
22
inject.js
22
inject.js
|
@ -23,6 +23,13 @@ tests = {
|
|||
};
|
||||
|
||||
},
|
||||
isPageHTTPS: function() {
|
||||
return location.protocol === "https:"
|
||||
},
|
||||
usesPiwik: function() {
|
||||
return !!this.piwikJSScriptObject();
|
||||
},
|
||||
// -------------
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
|
@ -30,15 +37,22 @@ tests = {
|
|||
return this.scriptObject.getAttribute("src");
|
||||
},
|
||||
piwikUsesHTTPS: function() {
|
||||
return this.parseURL(this.URLtoPiwikJS())["protocol"] === "https:"
|
||||
return this.parseURL(this.URLtoPiwikJS()).protocol === "https:"
|
||||
},
|
||||
isURLprotocolRelative: function() {
|
||||
return this.URLtoPiwikJS().startsWith("//");
|
||||
},
|
||||
isMixedContent: function() {
|
||||
return this.isPageHTTPS() && !this.piwikUsesHTTPS();
|
||||
},
|
||||
|
||||
isScriptAsync: function() {
|
||||
return this.scriptObject.hasAttribute("async") && this.scriptObject.hasAttribute("defer")
|
||||
},
|
||||
|
||||
isPageUTF8: function() {
|
||||
return document.characterSet.toLowerCase() === "utf-8";
|
||||
},
|
||||
main: function() {
|
||||
this.scriptObject = this.piwikJSScriptObject();
|
||||
if (!this.scriptObject) {
|
||||
|
@ -49,9 +63,11 @@ tests = {
|
|||
UrltoPiwikJs: this.URLtoPiwikJS(),
|
||||
piwikUsesHTTPS: this.piwikUsesHTTPS(),
|
||||
isURLprotocolRelative: this.isURLprotocolRelative(),
|
||||
isMixedContent: this.isMixedContent(),
|
||||
isScriptAsync: this.isScriptAsync(),
|
||||
isPageUTF8: this.isPageUTF8()
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
willBeSendToBackgroundJs = tests.main();
|
||||
chrome.runtime.sendMessage({action: "test", usesPiwik: tests.usesPiwik()});
|
||||
// willBeSendToBackgroundJs = tests.main();
|
|
@ -15,5 +15,22 @@
|
|||
"scripts": [
|
||||
"background.js"
|
||||
]
|
||||
}
|
||||
},
|
||||
"browser_action": {
|
||||
"default_title": "Google Mail",
|
||||
// optional; shown in tooltip
|
||||
"default_popup": "popup.html"
|
||||
// optional
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"js": [
|
||||
"inject.js"
|
||||
],
|
||||
"matches": [
|
||||
"*://*/*"
|
||||
],
|
||||
"run_at": "document_end"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<h2>DevTools panel</h2>
|
||||
<button id="start">Static Test</button>
|
||||
<button id="start">Static Test </button>
|
||||
<pre id="response"></pre>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Reference in a new issue