Skip to content

Instantly share code, notes, and snippets.

@NV
Last active December 30, 2015 05:29
Show Gist options
  • Save NV/412ab229632d34b5f15e to your computer and use it in GitHub Desktop.
Save NV/412ab229632d34b5f15e to your computer and use it in GitHub Desktop.
Chrome DevTools extension: contentEditable=on

Chrome DevTools Extension Panel sample

Adds a button to turn on contentEditable on the inspected webpage.

Injects content script lazyly.

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'run') {
chrome.tabs.executeScript(request.tabId, {
file: 'content-script.js'
}, function() {
console.log('content-script.js executed');
});
}
});
document.body.contentEditable = true;
<script src="devtools.js"></script>
chrome.devtools.panels.create("Edit",
"icon_32.png",
"panel.html",
function(panel) {
var shown = false;
panel.onShown.addListener(function(panelWindow) {
if (shown) return;
shown = true; // Run code below only once
var startButton = panelWindow.document.getElementById('start_button');
startButton.addEventListener('click', function() {
chrome.runtime.sendMessage({
action: "run",
tabId: chrome.devtools.inspectedWindow.tabId
});
}, false);
});
}
);
{
"name": "Chrome DevTools Panel Extension",
"version": "1.0",
"manifest_version": 2,
"description": "",
"icons": {
"32": "icon_32.png",
"48": "icon_48.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"<all_urls>"
],
"web_accessible_resources": [
"content-script.js"
],
"devtools_page": "devtools.html"
}
html {
background: #f3f3f3;
}
#start_button {
font-size: 16px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="panel.css">
</head>
<body>
<input type="button" id="start_button" value="Turn editing on">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment