マニフェスト.json:
{
"name": "permenu",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_icon": {
"19": "19icon.png"
},
"default_title": "permenu"
},
"background": {
"scripts": ["extension.js", "jquery-2.0.0.js"],
"persistent": true
},
"permissions": [
"tabs"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["browser.js", "jquery-2.0.0.js"],
"all_frames": true
}
]
}
拡張子.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, {command: "add" }, function(response) {
console.log(response.toString());
});
});
});
browser.js:
chrome.runtime.onMessage.addListener(function(msg) {
if (msg.command == "add") {
doAdd("click");
}
});
document.onreadystatechange = function() {
if(document.readyState == "complete") {
doAdd("onreadystatechange");
}
};
function doAdd(reason) {
if(reason == "click") {
if(document.getElementById("add") == null) {
localStorage[location.href] = "ready";
$("body")
.append("<ul id='menu'><li><a id='add' href='#'>Add div</a></li></ul>");
}
} else if(reason == "onreadystatechange") {
if(localStorage[location.href] == "ready") {
if(document.getElementById("add") == null) {
$("body")
.append(
"<ul id='menu'><li><a id='add' href='#'>Add div</a></li></ul>");
}
}
}
}