Skip to content
Permalink
Browse files

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 #41
  • Loading branch information...
pmarks-net committed Sep 6, 2018
1 parent e4bae48 commit f91c7da5bedca80a1e95a6e9a3b4f9ef7dccacd0
Showing with 48 additions and 32 deletions.
  1. +22 −17 src/background.js
  2. +1 −1 src/manifest.json
  3. +16 −0 src/popup.html
  4. +9 −14 src/popup.js
@@ -85,8 +85,9 @@ const TAB_DELETED = 2; // Dead.
// RequestFilter for webRequest events.
const FILTER_ALL_URLS = { urls: ["<all_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 --

@@ -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",
@@ -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;
}
</style>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="popup.js"></script>
@@ -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)) {

0 comments on commit f91c7da

Please sign in to comment.
You can’t perform that action at this time.