LP#1646166 Use content_scripts; no declarativeContent
authorBill Erickson <berickxx@gmail.com>
Fri, 27 Jan 2017 15:35:11 +0000 (10:35 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 16 Feb 2017 20:27:01 +0000 (15:27 -0500)
Configure content_scripts in the extension manifest to specify which
sites should receive the content script.  This allows us to dictate at
what stage of page loading the content script is inserted.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
extension/app/content.js
extension/app/extension.js
extension/app/manifest.json

index d9c70b4..d19d4f9 100644 (file)
  * 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");
 }
 
 /**
@@ -37,12 +39,6 @@ var port = chrome.runtime.connect();
  * 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);
 });
 
@@ -62,15 +58,9 @@ window.addEventListener("message", function(event) {
     // 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);
 
 
-
index 0a98e3e..fddd095 100644 (file)
@@ -106,50 +106,6 @@ chrome.runtime.onConnect.addListener(function(port) {
     });
 });
 
-
-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();
 
index ec39606..8e8c765 100644 (file)
@@ -8,15 +8,18 @@
   "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"
 }