Skip to content

Commit

Permalink
Merge pull request #187 from matatk/splash
Browse files Browse the repository at this point in the history
Initial attempt at splash screen
  • Loading branch information
matatk authored Jul 29, 2018
2 parents 91b456d + 4238b6f commit eed0ef4
Show file tree
Hide file tree
Showing 9 changed files with 1,709 additions and 1,889 deletions.
3,299 changes: 1,431 additions & 1,868 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "landmarks",
"version": "2.3.1",
"version": "2.4.0",
"private": true,
"scripts": {
"pre_build": "npm test",
Expand Down Expand Up @@ -28,16 +28,16 @@
"test:landmarks": "node test/test-landmarks.js"
},
"devDependencies": {
"addons-linter": "~1.1",
"addons-linter": "~1.2",
"archiver": "~2.1",
"chalk": "~2.4",
"deepmerge": "~2.1",
"eslint": "~4.19",
"fs-extra": "~6.0",
"eslint": "~5.2",
"fs-extra": "~7.0",
"husky": "~0.14",
"jsdom": "~11.11",
"jsdom": "~11.12",
"one-svg-to-many-sized-pngs": "github:matatk/one-svg-to-many-sized-pngs#0.1.1",
"puppeteer": "~1.5",
"puppeteer": "~1.6",
"replace-in-file": "~3.4",
"rimraf": "~2.6",
"test": "~0.6"
Expand Down
28 changes: 18 additions & 10 deletions src/assemble/manifest.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,40 @@
"content.pausing.js",
"content.supervisor.js"
]
},
{
"matches": ["http://matatk.agrip.org.uk/landmarks/"],
"js": ["splash.js"],
"css": ["splash.css"]
}
],

"commands": {
"_execute_browser_action": {
"suggested_key": {
"default": "Alt+Shift+L"
}
"show-all-landmarks": {
"description": "__MSG_showAllLandmarksShortcutDescription__"
},
"next-landmark": {
"main-landmark": {
"suggested_key": {
"default": "Alt+Shift+N"
"default": "Alt+Shift+M"
},
"description": "__MSG_nextLandmarkShortcutDescription__"
"description": "__MSG_mainLandmarkShortcutDescription__"
},
"prev-landmark": {
"suggested_key": {
"default": "Alt+Shift+P"
},
"description": "__MSG_previousLandmarkShortcutDescription__"
},
"main-landmark": {
"next-landmark": {
"suggested_key": {
"default": "Alt+Shift+M"
"default": "Alt+Shift+N"
},
"description": "__MSG_mainLandmarkShortcutDescription__"
"description": "__MSG_nextLandmarkShortcutDescription__"
},
"_execute_browser_action": {
"suggested_key": {
"default": "Alt+Shift+L"
}
}
},

Expand Down
8 changes: 8 additions & 0 deletions src/assemble/manifest.firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@
"id": "ariaLandmarkNav@ibm.com",
"strict_min_version": "56.0"
}
},

"commands": {
"show-all-landmarks": {
"suggested_key": {
"default": "Alt+Shift+S"
}
}
}
}
4 changes: 4 additions & 0 deletions src/static/_locales/en_GB/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"message": "Move to and highlight the main landmark"
},

"showAllLandmarksShortcutDescription": {
"message": "Toggle the outline on all landmarks"
},

"noMainLandmarkFound": {
"message": "No main landmark found"
},
Expand Down
21 changes: 18 additions & 3 deletions src/static/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ browser.commands.onCommand.addListener(function(command) {
case 'next-landmark':
case 'prev-landmark':
case 'main-landmark':
case 'show-all-landmarks':
sendToActiveTab({request: command})
break
}
Expand Down Expand Up @@ -91,7 +92,7 @@ browser.webNavigation.onHistoryStateUpdated.addListener(function(details) {


//
// Browser action badge updating
// Browser action badge updating; Command enumeration
//

// When the content script has loaded and any landmarks found, it will let us
Expand All @@ -101,10 +102,24 @@ browser.runtime.onMessage.addListener(function(message, sender) {
case 'update-badge':
landmarksBadgeUpdate(sender.tab.id, message.landmarks)
break
case 'get-commands':
browser.commands.getAll(async function(commands) {
sendToActiveTab({
request: 'splash-populate-commands',
commands: commands
})
})
break
case 'splash-open-configure-shortcuts':
// FIXME TODO disable for !Chrome
browser.tabs.create({
url: 'chrome://extensions/configureCommands'
})
break
default:
throw Error(
'Landmarks: background script received unknown message:',
message, 'from', sender)
'Landmarks: background script received unexpected request '
+ message.request + ' from tab ' + sender.tab.id)
}
})

Expand Down
7 changes: 5 additions & 2 deletions src/static/content.supervisor.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ function messageHandler(message, sender, sendResponse) {
findLandmarksAndUpdateBadge()
break
default:
throw Error('Landmarks: content script received unknown message: '
+ message.request)
if (!message.request || !message.request.startsWith('splash-')) {
throw Error(
'Landmarks: content script received unexpected request: '
+ message.request)
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/static/splash.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
td.error {
background-color: #d00;
color: white;
font-weight: bold;
}
214 changes: 214 additions & 0 deletions src/static/splash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
'use strict'
// FIXME localise (remember: table headings, button contents, ...)
// FIXME what to do when content script is re-loaded and disconnected in Blink

const chromeLike = window.chrome ? true : false

const splashPage = {
contains: [
// more stuff is added later
]
}

const shortcutTableRows = [
{
element: 'tr',
contains: [
{ element: 'th', content: 'Action' },
{ element: 'th', content: 'Keyboard shortcut' }
]
}
]

const chromeKeyboardShortcutsButton = {
element: 'p', contains: [{
element: 'button',
content: 'Add or change shortcuts',
listen: {
event: 'click',
handler: () => browser.runtime.sendMessage({
request: 'splash-open-configure-shortcuts'
})
}
}]
}


function makePart(structure, root) {
let newPart

for (const key in structure) {
switch (key) {
case 'element':
newPart = document.createElement(structure[key])
root.appendChild(newPart)
break
case 'class':
newPart.classList.add(structure[key])
break
case 'text':
root.appendChild(document.createTextNode(structure[key]))
break
case 'content':
newPart.appendChild(document.createTextNode(structure[key]))
break
case 'listen':
newPart.addEventListener(
structure[key].event, structure[key].handler)
break
case 'contains':
for (const contained of structure[key]) {
makePart(contained, newPart ? newPart : root)
}
break
default:
throw Error(`Unexpected structure key ${key} encountered.`)
}
}

return root
}


function addCommandRowAndReportIfMissing(command) {
// Work out the command's friendly name
let action

if (command.name === '_execute_browser_action') {
action = 'Show pop-up'
} else if (chromeLike) {
// Chrome returns the full descriptions
action = command.description
} else {
// Firefox requires the descriptions to be translated
const messageName = command.description.slice(6, -2)
action = browser.i18n.getMessage(messageName)
}

// Work out the command's shortcut
let shortcutCellElement

if (command.shortcut) {
// Firefox gives "Alt+Shift+N" but Chrome gives ⌥⇧N
if (chromeLike) {
shortcutCellElement = { element: 'td', contains: [
{ element: 'kbd', content: command.shortcut }
]}
} else {
shortcutCellElement = { element: 'td', contains:
firefoxShortcutElements(command.shortcut)
}
}
} else {
shortcutCellElement = { element: 'td', class: 'error', contains: [
{ text: 'Not set up' }
]}
}

shortcutTableRows.push({
element: 'tr',
contains: [
{ element: 'td', content: action },
shortcutCellElement
]
})
}


function firefoxShortcutElements(shortcut) {
const shortcutElements = []
const shortcutParts = shortcut.split(/(\+)/)

for (const keyOrPlus of shortcutParts) {
if (keyOrPlus !== '+') {
shortcutElements.push({ element: 'kbd', content: keyOrPlus })
} else {
shortcutElements.push({ text: ' + ' })
}
}

return shortcutElements
}


browser.runtime.onMessage.addListener(function(message) {
if (message.request !== 'splash-populate-commands') return

// Chrome allows only four keyboard shortcuts to be specified in the
// manifest; Firefox allows many.
//
// The extra ones for Firefox are patched in via its specific manifest file
// when the manifest is merged.
//
// The commands are in the manifest in the opposite order to that which
// seems most logical, and need to be reversed to pop out in the right
// order on the splash page. This is because the merging in of the extra
// keyboard shortcuts means that the commands with added keyboard shortucts
// in Firefox are bumped to the top of the commands object.
//
// What is a bit odd is that, on Chrome, it appears the reversal is not
// needed.
const commandsInOrder = chromeLike ?
message.commands : message.commands.reverse()

for (const command of commandsInOrder) {
addCommandRowAndReportIfMissing(command)
}

splashPage.contains.push({
element: 'table',
contains: shortcutTableRows
})

if (chromeLike) splashPage.contains.push(chromeKeyboardShortcutsButton)


// Create new bits

const heading = document.getElementById('via-shortcut-key')
const parent = heading.parentNode
const splashContainer = document.createElement('div')

parent.insertBefore(
makePart(splashPage, splashContainer), heading.nextSibling)
})


function main() {
// Check for various README elements and escape if things aren't as expected

const installationTocEntry = document.getElementsByTagName('li')[0]
if (!installationTocEntry
|| installationTocEntry.textContent !== 'Installation') return

const installationHeading = document.getElementById('installation')
if (!installationHeading) return

const shortcutKeysHeading = document.getElementById('via-shortcut-key')
if (!shortcutKeysHeading) return

// Remove installation section

installationTocEntry.remove()

while (installationHeading.nextSibling.tagName !== 'H2') { // FIXME H
installationHeading.nextSibling.remove()
}

installationHeading.remove()

// Remove static keyboard shortcuts section content

while (shortcutKeysHeading.nextSibling.tagName !== 'H3') { // FIXME H
shortcutKeysHeading.nextSibling.remove()
}

// Kickstart process to get commands and create new HTML

browser.runtime.sendMessage({
request: 'get-commands'
})
}


main()

0 comments on commit eed0ef4

Please sign in to comment.