I'm looking for a method of modifying current tab content (inject
HTML, CSS, JavaScript) when user click on an element in page action
popup.
For example:
popup.html
<a href="#" onclick="modifyPage">modify</a>
When user clicks on this link the page should be modified.
As far as I got it, I should use contentscript.js somehow, probably
subscribe on onRequest event like this:
chrome.extension.onRequest.addListener(function ()
{ document.body.innerHTML = ''; });
and then send request from the modifyPage method. But this approach
does not work. Any other one?
Thanks,
Alex
--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
Thank you for the answer. I've read the articles that you point on but
still have not good understanding of what should I do to pass message
from popup to content script.
Here is a complete example of what I'm trying to do:
manifest.js
{
"name": "GCETest",
"version": "1.0",
"description": "GCETest",
"background_page" : "background.html",
"page_action": {
"default_icon": "icon.png", "default_title": "Click me...",
"default_popup": "popup.html"
},
"content_scripts": [ { "matches": ["http://*/*"], "js":
["contentscript.js"] } ],
"permissions": ["tabs"]
}
background.html
<!DOCTYPE html>
<html>
<head>
<script>
function onRequest(request, sender, sendResponse) {
console.log('background.html: ' + request.action);
if (request.action == 'show')
chrome.pageAction.show(sender.tab.id);
sendResponse({});
};
console.log('background.html: setup "onRequest" listener');
chrome.extension.onRequest.addListener(onRequest);
</script>
</head>
</html>
contentscript.js
console.log('contentscript.js: setup "onRequest" listener');
chrome.extension.onRequest.addListener(function (request, sender,
sendResponse) {
console.log('contentscript.js: ' + request.action);
if (request.action == 'modify')
document.body.appendChild(
document.createTextNode('This does not work...'));
sendResponse({});
});
console.log('contentscript.js: sending "show" request');
chrome.extension.sendRequest({action:'show'}, function(response) {});
popup.html
<html>
<head>
<script>
function send() {
console.log('popup.html: sending "modify" request');
chrome.extension.sendRequest({action:'modify'}, function(response)
{});
window.close();
}
</script>
</head>
<body><a href="#" onclick="send();">Click me...</a></body>
</html>
The workflow is simple:
1. background.html attaches listener on onRequest that enables page
action.
2. contentscript.js attaches listener on onRequest and sends a request
that enables page action.
I'm suspecting that both listeners should be called, but it did not
happen. Only listener attached by background.html is notified.
3. popup.html should send request which should notify listeners but
only listener attached by background.html is notified again.
I think that I do something wrong because listener attached by
contentscript.js is not notified, but I do not know what exactly I'm
doing wrong.
Maybe popup.html should notify content script in some other way or
background.html should modify the DOM or maybe background.html should
resend request?
Thanks,
Alex
On Apr 13, 12:47 am, Mohamed Mansour <m0.interact...@gmail.com> wrote:
> Hi Alex,
>
> You could leverage executeScript and insertCSS:http://code.google.com/chrome/extensions/tabs.html#method-executeScript
> <http://code.google.com/chrome/extensions/tabs.html#method-executeScript>http://code.google.com/chrome/extensions/tabs.html#method-insertCSS
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {action:'modify'}, function(response) {});
});
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
Sincerely,
Alex
> > <chromium-extensions%2Bunsubscr...@chromium.org<chromium-extensions%252Bunsubscr...@chromium.org>