* Relays messages between the browser tab and the Hatch extension.js
* script.
*/
-
console.debug('Loading Hatch relay content script');
-// document.body can be null in rare cases, e.g. quick page reload.
-if (document.body) {
-
+// Insert our calling card in the document. This script loads before the
+// DOM is rendered. The root documentElement is the only thing we can
+// attach to.
+if (document.documentElement) {
// Tell the page DOM we're here.
- document.body.setAttribute('hatch-is-open', '4-8-15-16-23-42');
+ document.documentElement.setAttribute('hatch-is-open', '4-8-15-16-23-42');
+} else {
+ console.warn("No document.documentElement exist, Hatch cannot open");
}
/**
* Relay all messages received from the extension back to the tab
*/
port.onMessage.addListener(function(message) {
-
- /*
- console.debug(
- "Content script received from extension: "+ JSON.stringify(message));
- */
-
window.postMessage(message, location.origin);
});
// received from our browser tab/page.
if (message.from != 'page') return;
- /*
- console.debug(
- "Content script received from page: " + JSON.stringify(message));
- */
-
// standard Hatch-bound message; relay to extension.
port.postMessage(message);
}, false);
-
});
});
-
-function setPageActionRules() {
- // Replace all rules on extension reload
- chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
- chrome.declarativeContent.onPageChanged.addRules([
- {
- conditions: [
- new chrome.declarativeContent.PageStateMatcher({
- pageUrl : {
- pathPrefix : '/eg/staff/',
- schemes : ['https']
- },
- // match <body hatch-is-welcome-here>...
- css: ["body[hatch-is-welcome-here]"]
- })
- ],
- actions: [
- new chrome.declarativeContent.RequestContentScript({
- 'js': ['content.js']
- })
- ]
- }
- ]);
- });
-}
-
-chrome.browserAction.onClicked.addListener(function (tab) {
- chrome.permissions.request({
- origins: ['https://*/eg/staff/*']
- }, function (ok) {
- if (ok) {
- console.log('access granted');
- } else if (chrome.runtime.lastError) {
- alert('Permission Error: ' + chrome.runtime.lastError.message);
- } else {
- alert('Optional permission denied.');
- }
- });
-});
-
-
-// Link the page action icon to loading the content script
-chrome.runtime.onInstalled.addListener(setPageActionRules);
-
// Connect to Hatch on startup.
connectToHatch();
"background" : {
"scripts" : ["extension.js"]
},
+ "content_scripts": [
+ {
+ "matches": ["https://*/eg/staff/*"],
+ "js": ["content.js"],
+ "run_at": "document_start"
+ }
+ ],
"browser_action": {
"default_title": "Hatch"
},
"permissions": [
- "nativeMessaging",
- "declarativeContent"
- ],
- "optional_permissions": [
- "https://*/eg/staff/*"
+ "nativeMessaging"
],
"minimum_chrome_version": "38"
}