Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Manifest to V3, include mixed-mode tudor crown icons #186

Merged
merged 8 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ShowMetaTagsComponent.prototype.hideMetaTags = function () {

ShowMetaTagsComponent.prototype.sendState = function () {
chrome.runtime.sendMessage({
action: 'showMetaTagState',
showMetaTagsState: this.isMetaTagsDisplayed
action: 'showMetaTagsState',
metaTagsState: this.isMetaTagsDisplayed
})
}
15 changes: 0 additions & 15 deletions src/events/icon.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/fetch-page-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chrome.runtime.sendMessage({
renderingApplication: getMetatag('govuk:rendering-application'),
abTestBuckets: getAbTestBuckets(),
windowHeight: window.innerHeight,
highlightState: window.highlightComponent.state
highlightState: false // window.highlightComponent.state
})

function getMetatag (name) {
Expand Down
Binary file removed src/icons/crown-logo-19-active-dark-mode.png
Binary file not shown.
Binary file modified src/icons/crown-logo-19-active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/icons/crown-logo-19-inactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/icons/crown-logo-38-active-dark-mode.png
Binary file not shown.
Binary file modified src/icons/crown-logo-38-active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/icons/crown-logo-38-inactive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 19 additions & 16 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "GOV.UK Browser Extension",
"description": "Switch between GOV.UK environments and content",
"homepage_url": "https://github.com/alphagov/govuk-browser-extension",
"version": "1.22.0",
"version": "1.23.0",
"content_scripts": [
{
"matches": ["https://*.gov.uk/*"],
"matches":
[
"https://*.gov.uk/*"
],
"all_frames": true,
"js": [
"popup/lib/jquery.min.js",
"popup/lib/mustache.min.js",
"components/highlight-component/highlight-component.js",
"components/design-mode-component/design-mode-component.js",
Expand All @@ -27,26 +30,26 @@
"128": "icons/128.png"
},
"permissions": [
"http://*.gov.uk/*",
"https://*.gov.uk/*",
"cookies",
"declarativeContent",
"scripting",
"tabs",
"webRequest",
"webRequestBlocking"
"webRequest"
],
"page_action": {
"background": {
"service_worker": "service_worker.js"
},
"action": {
"default_icon": {
"19": "icons/crown-logo-19-inactive.png",
"38": "icons/crown-logo-38-inactive.png"
},
"default_title": "GOV.UK",
"default_popup": "popup.html"
},
"background": {
"scripts": [
"events/icon.js",
"events/ab_bucket_store.js",
"events/ab_test_settings.js"
]
}
"content_security_policy": {},
"host_permissions": [
"http://*.gov.uk/*",
"https://*.gov.uk/*"
]
}
1 change: 0 additions & 1 deletion src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="utf-8">
<script src='popup/lib/mustache.min.js'></script>
<script src='popup/lib/jquery.min.js'></script>

<script src='popup/ab_tests.js'></script>
<script src='popup/content_links.js'></script>
Expand Down
8 changes: 8 additions & 0 deletions src/popup/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ Popup.environment = function (location, host, origin) {
allEnvironments: {
name: 'GOV.UK',
url: 'https://www.gov.uk'
},
currentEnvironment: {
name: 'Production',
protocol: 'https',
serviceDomain: 'publishing.service.gov.uk',
host: 'https://www.gov.uk',
origin: origin
}

}
}

Expand Down
2 changes: 0 additions & 2 deletions src/popup/lib/jquery.min.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIce work removing this!

This file was deleted.

70 changes: 37 additions & 33 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ var Popup = Popup || {};
(function () {
// Execute a script on the main thread (which has access to the currently
// loaded page). This script will call back to us.
document.addEventListener('DOMContentLoaded', () => {
chrome.tabs.executeScript(null, {
code: 'window.highlightComponent = window.highlightComponent || new HighlightComponent; undefined;'
})

chrome.tabs.executeScript(null, {
code: 'window.designModeComponent = window.designModeComponent || new DesignModeComponent; undefined;'
})

chrome.tabs.executeScript(null, {
code: 'window.showMetaTagsComponent = window.showMetaTagsComponent || new ShowMetaTagsComponent; undefined;'
document.addEventListener('DOMContentLoaded', async () => {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);

chrome.scripting.executeScript({
target: {tabId: tab.id},
func: () => {
window.highlightComponent = window.highlightComponent || new HighlightComponent
window.designModeComponent = window.designModeComponent || new DesignModeComponent
window.showMetaTagsComponent = window.showMetaTagsComponent || new ShowMetaTagsComponent
}
})

chrome.tabs.executeScript(null, {
file: 'fetch-page-data.js'
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['fetch-page-data.js']
})
})

Expand All @@ -29,8 +32,8 @@ var Popup = Popup || {};
if (request.action === 'populatePopup') {
// When we're asked to populate the popup, we'll first send the current
// buckets back to the main thread, which "persists" them.
var abTestSettings = chrome.extension.getBackgroundPage().abTestSettings
var abTestBuckets = abTestSettings.initialize(request.abTestBuckets, request.currentLocation)
//var abTestSettings = chrome.extension.getBackgroundPage().abTestSettings
// var abTestBuckets = abTestSettings.initialize(request.abTestBuckets, request.currentLocation)

renderPopup(
request.currentLocation,
Expand All @@ -39,7 +42,7 @@ var Popup = Popup || {};
request.currentPathname,
request.renderingApplication,
request.windowHeight,
abTestBuckets
null // abTestBuckets
)
}
})
Expand All @@ -53,10 +56,10 @@ var Popup = Popup || {};
})

chrome.runtime.onMessage.addListener(function (request, _sender) {
if (request.action === 'showMetaTagState') {
if (request.action === 'showMetaTagsState') {
// When we're asked to populate the popup, we'll first send the current
// buckets back to the main thread, which "persists" them.
if (request.metaTags) { document.querySelector('#highlight-meta-tags').textContent = 'Hide meta tags' } else { document.querySelector('#highlight-meta-tags').textContent = 'Show meta tags' }
if (request.metaTagsState) { document.querySelector('#highlight-meta-tags').textContent = 'Hide meta tags' } else { document.querySelector('#highlight-meta-tags').textContent = 'Show meta tags' }
}
})

Expand Down Expand Up @@ -100,24 +103,25 @@ var Popup = Popup || {};
}
}

function renderView (view, currentUrl) {
async function renderView (view, currentUrl) {
var template = document.querySelector('#template').innerHTML
var popupContent = document.querySelector('#content')
popupContent.innerHTML = Mustache.render(template, view)
setupClicks()

setupAbToggles(currentUrl)

chrome.tabs.executeScript(null, {
code: 'window.highlightComponent.sendState(); undefined;'
})

chrome.tabs.executeScript(null, {
code: 'window.showMetaTagsComponent.sendState(); undefined;'
})

chrome.tabs.executeScript(null, {
code: 'window.designModeComponent.sendState(); undefined;'
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);

chrome.scripting.executeScript({
target: {tabId: tab.id},
func: () => {
window.highlightComponent.sendState()
window.showMetaTagsComponent.sendState()
window.designModeComponent.sendState()
}
})
}

Expand Down Expand Up @@ -158,7 +162,7 @@ var Popup = Popup || {};
// TODO: we're not actually re-rendering the popup correctly here, because
// we don't have access to the DOM here. This is a temporary solution to
// make most functionality work after the user clicks a button in the popup.
renderPopup(location, '', {})
renderPopup(location.href, location.host, location.origin, location.pathname, {})
})
})

Expand Down Expand Up @@ -201,10 +205,10 @@ var Popup = Popup || {};
testBucket.classList.remove('ab-bucket-selected')
}
abTestBucket.classList.add('ab-bucket-selected')
chrome.tabs.reload(null, { bypassCache: true })
chrome.tabs.reload(tab.id, { bypassCache: true })
}
)
})
1 })
})
}

Expand All @@ -214,15 +218,15 @@ var Popup = Popup || {};
function createView (location, host, origin, pathname, renderingApplication, abTestBuckets) {
var environment = Popup.environment(location, host, origin)
var contentLinks = Popup.generateContentLinks(location, origin, pathname, environment.currentEnvironment, renderingApplication)
var abTests = Popup.findActiveAbTests(abTestBuckets)
// var abTests = Popup.findActiveAbTests(abTestBuckets)

return {
environments: environment.allEnvironments,
currentEnvironment: environment.currentEnvironment,
contentLinks: contentLinks,
// external links will be populated by a call to the content store
externalLinks: [],
abTests: abTests
abTests: [] // abTests
}
}

Expand Down
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions src/service_utils/icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This script runs in the service worker in Chrome. It will activate the small
// greyed out GOV.UK logo in the Chrome menu bar whenever we're on a gov.uk page.
chrome.declarativeContent.onPageChanged.removeRules(async () => {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: 'www.gov.uk' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: 'dev.gov.uk' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostSuffix: 'publishing.service.gov.uk' },
}),
],
actions: [
new chrome.declarativeContent.SetIcon({
imageData: {
19: await loadImageData("icons/crown-logo-19-active.png"),
38: await loadImageData("icons/crown-logo-38-active.png"),
},
}),
chrome.declarativeContent.ShowAction
? new chrome.declarativeContent.ShowAction()
: new chrome.declarativeContent.ShowPageAction(),
],
}]);
});

async function loadImageData(url) {
const img = await createImageBitmap(await (await fetch(url)).blob());
const {width: w, height: h} = img;
const canvas = new OffscreenCanvas(w, h);
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, w, h);
return ctx.getImageData(0, 0, w, h);
}
1 change: 1 addition & 0 deletions src/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
importScripts("./service_utils/icon.js")
Loading