javascript - Adding HTML to a page with a Chrome Extension -
okay here's want extension do. want add html page (label, button whatever).
here's have...
content.js (it tries add label not button)
chrome.extension.onrequest.addlistener(function(request, sender, sendresponse) { if (request.action == "addbutton") { addbuttonhtml(); } }); function addbuttonhtml() { $('body').append("<label>hhhhhhhhhhhhhhhhhhhhh</label>"); } background.js
addbutton("addbutton"); function addbutton(action) { chrome.tabs.getselected(null, function(tab) { chrome.tabs.sendrequest(tab.id, { action: action, tabid: tab.id }, function(response) {}); }); } my question is, doing wrong? , should manifest have of now.
manifest.json
{ "name": "extension", "version": "1.0", "description": "does stuff", "background": { "scripts": ["background.js"], "persistent": false }, "page_action": { "default_icon": "icon-19.png" }, "permissions": [ "http://*/*", "https://*/*", "tabs" ], "icons": { "48": "icon-48.png", "128": "icon-128.png" }, "content_scripts": [{ "matches": ["http://*/*", "https://*/*"], "js": ["content.js"] }], "manifest_version": 2 } thank you
you indicated want on pages determine in background.js. means can remove content script manifest, , think able change tabs permission activetab. there still question of whether label should loaded on these pages, or after user action.
if label should there, want background.js (which run on every page load) call chrome.tabs.executescript load content.js (reference), no longer needs listener added.
if label should appear result of user action, sounds want page action. in addition above, background.js should call chrome.pageaction.show(tabid) show page action icon, , use chrome.pageaction.onclicked.addlistener(loadscriptfunction) call executescript.
edit: think able diagnose attempt went wrong. google doesn't specify when background.js executes, seems (extension startup?), , not after content.js. if wrap bare addbutton("addbutton") inside of chrome.pageaction.onclicked.addlistener, appears work.
Comments
Post a Comment