I am writing a Google Chrome extension to automate some common tasks. The functionality I want is as follows:
I have completed steps 1,2,and 3 and they work. I am having a lot of trouble trying to listen for the url change after my credentials are submitted so that the function that selects roundcube client can run
I know I can run a script when client selection page appears by adding to my manifest but I want to use "chrome.tabs.executeScript" instead so that roundcube is chosen only if I run the script from the chrome extension and not if I go to client selection page manually.
Here is my manifest.json:
{ "manifest_version": 2, "name" : "Chrome Autobot", "description": "This extension will run various automation scripts for google chrome", "version" : "1.0", "browser_action" : { "default_icon" : "icon.png", "default_popup": "index.html" }, "permissions": [ "activeTab", "webNavigation", "tabs", "http://*/*", "https://*/*" ] }
Here is my chrome script:
jQuery(function($) { "Use Strict"; var openWebmail = function() { chrome.tabs.create({ url: 'http://mywebmaillogin.com:2095/' }, function() { chrome.tabs.executeScript(null, {file: "scripts/openEmail.js"}); }); chrome.tabs.onUpdated.addListener(function(){ chrome.tabs.executeScript(null, {file: "scripts/openEmail.js"}); alert('i work'); }); }; var init = $('.script-init'); init.on('click', function() { openWebmail(); }); });
and here is the content script to be executed as a callback of tab creation (when the email login page is fetched and the DOM has loaded), and also when the email credentials are submitted and the client selection page's DOM has loaded (which is not working right now)
var openEmail = function() { var loc = window.location.href; if(loc === 'http://mywebmaillogin.com:2095/') { var submit = document.getElementById('login_submit'); user.value = 'myusername'; pass.value = 'mypassword'; if(user.value === 'myusername' && pass.value === 'mypassword') { submit.click(); } else { openEmail(); } } if(loc.indexOf('http://mywebmaillogin:2095/') > -1 && loc.indexOf('login=1') > -1) { alert('I work'); } }()
any help would be appreciated... thanks!
As mentioned by NycCompSci you cannot call the chrome api from content scripts. I was able to pass api data to content scripts with message passing though, so thought I'd share that here. First call onUpdated
in background.js:
{ "name": "My test extension", "version": "1", "manifest_version": 2, "background": { "scripts":["background.js"] }, "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["contentscript.js"] } ], "permissions": [ "tabs" ] }
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { // read changeInfo data and do something with it (like read the url) if (changeInfo.url) { // do something here } } );
Then you can expand that script to send data (including the new url and other chrome.tabs.onUpdated info) from background.js to your content script like this:
chrome.tabs.onUpdated.addListener( function(tabId, changeInfo, tab) { // read changeInfo data and do something with it // like send the new url to contentscripts.js if (changeInfo.url) { chrome.tabs.sendMessage( tabId, { message: 'hello!', url: changeInfo.url }) } } );
Now you just need to listen for that data in your content script:
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { // listen for messages sent from background.js if (request.message === 'hello!') { console.log(request.url) // new url is now in content scripts! } });
Hope that helps someone! ʘ‿ʘ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With