Playing around with building a Chrome extension. At the moment I've put together a context menu item. When the context menu item is clicked, it fires itemClicked()
in my background script context_menu.js
:
function itemClicked(info, tab) {
alert("clicked");
}
The alert fires. I can also do stuff like sending ajax requests through itemClicked()
However, I can't append any elements to the page (or DOM manipulation of any sort). Even something as basic as this doesn't work:
var d = document.createElement('div');
d.setAttribute("css", "width: 100px; height: 100px; background-color: red; position: fixed; top: 70px; left: 30px; z-index: 99999999999;");
document.body.appendChild(d);
So I tried to add the same code to a content script:
chrome.contextMenus.onClicked.addListener(function(OnClickData info, tabs.Tab tab) {
//code to append the input here
});
But it still won't work. What am I doing wrong?
How can I get the context menu to append something to the page after clicking?
Thanks so much!
Edit: here is my manifest.json (removed the irrelevant stuff like name/description...etc)
{
"permissions": [
"activeTab",
"tabs",
"cookies",
"contextMenus"
],
"background": {
"scripts": ["context_menu.js"]
},
"browser_action": {
"default_icon": "icon16.png",
"default_css": "popup.css",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["vendor/jquery-1.8.2.min.js", "config.js", "content_script.js"]
}
],
"web_accessible_resources": ["popup.html"]
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…