From: Bill Erickson Date: Fri, 27 Jan 2017 15:35:11 +0000 (-0500) Subject: LP#1646166 Use content_scripts; no declarativeContent X-Git-Tag: v0.3.2~38 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=008aa5743524ccdcaa2a8d3b4cf0724e2a2ba404;p=Hatch.git LP#1646166 Use content_scripts; no declarativeContent 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 --- diff --git a/extension/app/content.js b/extension/app/content.js index d9c70b4..d19d4f9 100644 --- a/extension/app/content.js +++ b/extension/app/content.js @@ -18,14 +18,16 @@ * 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); - diff --git a/extension/app/extension.js b/extension/app/extension.js index 0a98e3e..fddd095 100644 --- a/extension/app/extension.js +++ b/extension/app/extension.js @@ -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 ... - 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(); diff --git a/extension/app/manifest.json b/extension/app/manifest.json index ec39606..8e8c765 100644 --- a/extension/app/manifest.json +++ b/extension/app/manifest.json @@ -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" }