javascript - Chrome extension : DOM + ContextualMenu -



javascript - Chrome extension : DOM + ContextualMenu -

i interact dom of current page when click on event in contextualmenu chromeextension.

there code :

manifest.json

{ "name": "jquery dom", "version": "1", "permissions":[ "http://*/", "contextmenus" ], "description": "manipulate dom when page done loading", "background": { "scripts": ["sample.js", "jquery.min.js"]}, "browser_action": { "name": "manipulate dom", "icons": ["icon.png"], "default_icon": "icon.png" }, "content_scripts": [ { "js": [ "jquery.min.js", "background.js" ], "matches": [ "http://*/*", "https://*/*"] }], "manifest_version": 2 }

sample.js

// copyright (c) 2010 chromium authors. rights reserved. // utilize of source code governed bsd-style license can // found in license file. // generic onclick callback function. function editonclick(info, tab) { alert($("body").html()); } // create parent item , 2 children. var parent = chrome.contextmenus.create({"title": "therefore"}); var child1 = chrome.contextmenus.create( {"title": "Éditer", "parentid": parent, "onclick": editonclick});

when seek this, alert box:

<script src="sample.js"></script> <script src="jquery.min.js"></script>

but $("body").append('test') in background.js, can interact straight dom of page. don't know why can't contextualmenu.

i answers. had create event triggered content-script.js because it's not possible access dom page background.

there new code illustration :

background.js

chrome.tabs.query({active: true, currentwindow: true}, function(tabs) { chrome.tabs.sendmessage(tabs[0].id, {greeting: "hello"}, function(response) { console.log(response.farewell); }); });

contentscript.js

chrome.runtime.onmessage.addlistener( function(request, sender, sendresponse) { console.log(sender.tab ? "from content script:" + sender.tab.url : "from extension"); if (request.greeting == "hello") sendresponse({farewell: "goodbye"}); });

it's official documentation. when seek this, ve got error : error in event handler (unknown): cannot read property 'farewell' of undefined stack trace: typeerror: cannot read property 'farewell' of undefined

i don't know what's wrong simple example. it's ve got no reply contentscript.js

javascript jquery dom google-chrome-extension

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -