Skip to content

Instantly share code, notes, and snippets.

@mieko
Created October 11, 2016 07:59
Show Gist options
  • Save mieko/a6a16d239d8179fd8b771322dfa4706f to your computer and use it in GitHub Desktop.
Save mieko/a6a16d239d8179fd8b771322dfa4706f to your computer and use it in GitHub Desktop.
<script src="./bg.js"></script>
(function() {
var spawn_url = null;
var menu = null;
var viewBackgroundImage = function() {
window.open(spawn_url);
};
var toggleMenu = function(b) {
if (b) {
if (!menu) {
menu = chrome.contextMenus.create({ "title": "View Background Image",
"onclick": viewBackgroundImage,
"contexts": ['all']});
}
} else {
if (menu) {
chrome.contextMenus.remove(menu);
menu = null;
}
}
};
chrome.extension.onRequest.addListener(
function(req, sender, resp) {
if (req.url && req.url != 'none') {
var u = req.url.replace(/['"]/g, '').replace(/url\((.*)\);?/, '$1');
spawn_url = u;
toggleMenu(true);
} else {
toggleMenu(false);
}
}
);
})();
(function() {
document.addEventListener('mouseover', function(ev) {
var u = document.defaultView.getComputedStyle(ev.target, null).backgroundImage;
chrome.extension.sendRequest({"url": u || false });
}, true);
})();
{
"name": "View Background Image",
"version": "1.0",
"description": "Add 'View Background Image' to the context menu",
"permissions": ["contextMenus", "tabs"],
"background_page": "background.html",
"content_scripts": [{"matches": ["<all_urls>"],
"run_at": "document_start",
"js": ["cs.js"]}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment