Skip to content

Commit

Permalink
Don't enable/disable the context menu.
Browse files Browse the repository at this point in the history
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 f91c7da
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
39 changes: 22 additions & 17 deletions src/background.js
Expand Up @@ -85,8 +85,9 @@ const TAB_DELETED = 2; // Dead.
// RequestFilter for webRequest events. // RequestFilter for webRequest events.
const FILTER_ALL_URLS = { urls: ["<all_urls>"] }; 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 IP_CHARS = /^[0-9A-Fa-f:.]+$/;
const DNS_CHARS = /^[0-9A-Za-z._-]+$/;


// Load spriteXX.png of a particular size. // Load spriteXX.png of a particular size.
// Executing this inline ensures that the images load before // Executing this inline ensures that the images load before
Expand Down Expand Up @@ -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(); window.popups = new Popups();


// -- TabTracker -- // -- TabTracker --
Expand Down Expand Up @@ -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, // 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 // so I'm open to making this a config option if someone recommends another
// useful non-spammy service. // 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({ const menuId = chrome.contextMenus.create({
enabled: menuIsEnabled, title: "Look up on bgp.he.net",
title: "Look up address on bgp.he.net",
// Scope the menu to text selection in our popup windows. // Scope the menu to text selection in our popup windows.
contexts: ["selection"], contexts: ["selection"],
documentUrlPatterns: [document.location.origin + "/popup.html"], documentUrlPatterns: [document.location.origin + "/popup.html"],
onclick: function(info) { onclick: function(info) {
const text = info.selectionText; const text = info.selectionText;
if (IP_CHARS.test(text)) { 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 -- // -- Options Storage --


Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
@@ -1,7 +1,7 @@
{ {
"name": "IPvFoo", "name": "IPvFoo",
"manifest_version": 2, "manifest_version": 2,
"version": "1.42", "version": "1.43",
"minimum_chrome_version": "26", "minimum_chrome_version": "26",
"description": "Display the server IP address, with a realtime summary of IPv4, IPv6, and HTTPS information across all page elements.", "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", "homepage_url": "https://github.com/pmarks-net/ipvfoo",
Expand Down
16 changes: 16 additions & 0 deletions src/popup.html
Expand Up @@ -65,6 +65,22 @@
font-weight: bold; font-weight: bold;
color: #888; 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> </style>
<script type="text/javascript" src="common.js"></script> <script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="popup.js"></script> <script type="text/javascript" src="popup.js"></script>
Expand Down
23 changes: 9 additions & 14 deletions src/popup.js
Expand Up @@ -67,6 +67,14 @@ function pushSpillCount(count) {
document.createTextNode(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) { function removeChildren(n) {
while (n.hasChildNodes()) { while (n.hasChildNodes()) {
n.removeChild(n.lastChild); n.removeChild(n.lastChild);
Expand Down Expand Up @@ -149,7 +157,7 @@ function makeRow(isFirst, tuple) {
addrTd.className = "ipCell" + addrClass + connectedClass; addrTd.className = "ipCell" + addrClass + connectedClass;
addrTd.appendChild(document.createTextNode(addr)); addrTd.appendChild(document.createTextNode(addr));
addrTd.onclick = handleClick; addrTd.onclick = handleClick;
addrTd.oncontextmenu = handleAddrContextMenu; addrTd.oncontextmenu = handleContextMenu;


// Build the (possibly invisible) "WebSocket/Cached" column. // Build the (possibly invisible) "WebSocket/Cached" column.
// We don't need to worry about drawing both, because a cached WebSocket // We don't need to worry about drawing both, because a cached WebSocket
Expand Down Expand Up @@ -209,19 +217,6 @@ function isSpuriousSelection(sel, newTimeStamp) {
return false; 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) { function handleContextMenu(e) {
const sel = window.getSelection(); const sel = window.getSelection();
if (isSpuriousSelection(sel, e.timeStamp)) { if (isSpuriousSelection(sel, e.timeStamp)) {
Expand Down

0 comments on commit f91c7da

Please sign in to comment.