From f91c7da5bedca80a1e95a6e9a3b4f9ef7dccacd0 Mon Sep 17 00:00:00 2001 From: Paul Marks Date: Thu, 6 Sep 2018 00:12:29 -0700 Subject: [PATCH] Don't enable/disable the context menu. On Chrome 70 (possibly earlier), the menu stopped responding to racing changes, so let's just leave the bgp.he.net option enabled, and decide what to do based on the selected text. Use bgp.he.net/dns for hostnames, per https://github.com/pmarks-net/ipvfoo/pull/41 --- src/background.js | 39 ++++++++++++++++++++++----------------- src/manifest.json | 2 +- src/popup.html | 16 ++++++++++++++++ src/popup.js | 23 +++++++++-------------- 4 files changed, 48 insertions(+), 32 deletions(-) diff --git a/src/background.js b/src/background.js index d1d5b3c..2ceca40 100644 --- a/src/background.js +++ b/src/background.js @@ -85,8 +85,9 @@ const TAB_DELETED = 2; // Dead. // RequestFilter for webRequest events. const FILTER_ALL_URLS = { urls: [""] }; -// Simple whitelist of IP address characters. +// Whitelist IP address and domain name characters. const IP_CHARS = /^[0-9A-Fa-f:.]+$/; +const DNS_CHARS = /^[0-9A-Za-z._-]+$/; // Load spriteXX.png of a particular size. // Executing this inline ensures that the images load before @@ -515,6 +516,13 @@ Popups.prototype.pushSpillCount = function(tabId, count) { } }; +Popups.prototype.shake = function(tabId) { + const win = this.map[tabId]; + if (win) { + win.shake(); + } +} + window.popups = new Popups(); // -- TabTracker -- @@ -770,33 +778,30 @@ chrome.webRequest.onErrorOccurred.addListener(forgetRequest, FILTER_ALL_URLS); // item to look up the address on bgp.he.net. I don't like picking favorites, // so I'm open to making this a config option if someone recommends another // useful non-spammy service. -let menuIsEnabled = false; +// +// Unless http://crbug.com/60758 gets resolved, the context menu's appearance +// cannot vary based on content. const menuId = chrome.contextMenus.create({ - enabled: menuIsEnabled, - title: "Look up address on bgp.he.net", + title: "Look up on bgp.he.net", // Scope the menu to text selection in our popup windows. contexts: ["selection"], documentUrlPatterns: [document.location.origin + "/popup.html"], onclick: function(info) { const text = info.selectionText; if (IP_CHARS.test(text)) { - chrome.tabs.create({url: "http://bgp.he.net/ip/" + text}); + chrome.tabs.create({url: "https://bgp.he.net/ip/" + text}); + } else if (DNS_CHARS.test(text)) { + chrome.tabs.create({url: "https://bgp.he.net/dns/" + text}); + } else { + // Malformed selection; shake the popup content. + const tabId = /#(\d+)$/.exec(info.pageUrl); + if (tabId) { + popups.shake(Number(tabId[1])); + } } } }); -// Enable the context menu iff the text might be an IP address. I think it's -// technically a race to do this from a contextmenu handler, but trivial updates -// seem to work okay. http://crbug.com/60758 would be helpful here. -function updateContextMenu(text) { - const enabled = IP_CHARS.test(text); - if (enabled == menuIsEnabled) { - return; - } - chrome.contextMenus.update(menuId, {enabled: enabled}); - menuIsEnabled = enabled; -} - // -- Options Storage -- diff --git a/src/manifest.json b/src/manifest.json index 18a56c6..18ae82c 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "name": "IPvFoo", "manifest_version": 2, - "version": "1.42", + "version": "1.43", "minimum_chrome_version": "26", "description": "Display the server IP address, with a realtime summary of IPv4, IPv6, and HTTPS information across all page elements.", "homepage_url": "https://github.com/pmarks-net/ipvfoo", diff --git a/src/popup.html b/src/popup.html index b27fae9..bbf5bb1 100644 --- a/src/popup.html +++ b/src/popup.html @@ -65,6 +65,22 @@ font-weight: bold; color: #888; } + +@keyframes shake { + from, to { + transform: translate3d(0, 0, 0); + } + 20%, 60% { + transform: translate3d(5px, 0, 0); + } + 40%, 80% { + transform: translate3d(-5px, 0, 0); + } +} +.shake { + animation-name: shake; + animation-duration: 500ms; +} diff --git a/src/popup.js b/src/popup.js index 521644b..9e6a8d6 100644 --- a/src/popup.js +++ b/src/popup.js @@ -67,6 +67,14 @@ function pushSpillCount(count) { document.createTextNode(count)); } +// Shake the content (for 500ms) to signal an error. +function shake() { + document.body.className = "shake"; + setTimeout(function() { + document.body.className = ""; + }, 600); +} + function removeChildren(n) { while (n.hasChildNodes()) { n.removeChild(n.lastChild); @@ -149,7 +157,7 @@ function makeRow(isFirst, tuple) { addrTd.className = "ipCell" + addrClass + connectedClass; addrTd.appendChild(document.createTextNode(addr)); addrTd.onclick = handleClick; - addrTd.oncontextmenu = handleAddrContextMenu; + addrTd.oncontextmenu = handleContextMenu; // Build the (possibly invisible) "WebSocket/Cached" column. // We don't need to worry about drawing both, because a cached WebSocket @@ -209,19 +217,6 @@ function isSpuriousSelection(sel, newTimeStamp) { return false; } -function handleAddrContextMenu(e) { - const sel = handleContextMenu.call(this, e); - const text = sel.toString(); - if (text == this.innerText) { - bg.updateContextMenu(text); - e.cancelBubble = true; // Inhibits the handler below. - } -} - -document.oncontextmenu = function() { - bg.updateContextMenu(""); -}; - function handleContextMenu(e) { const sel = window.getSelection(); if (isSpuriousSelection(sel, e.timeStamp)) {