0

I am trying to create a chrome extension to automate the login process.

I am able to login the website using Process_login2 (fill in the username and password and click login button).

However, when I use Process_login (with addon function to open tab and navigate to the webpage), the login function is failed.

Do you have any idea how to run the script only when the new tab is created and loaded?

window.addEventListener('DOMContentLoaded', function () {
  var Process_login = document.querySelector('#menu_btn_login');
  var Process_login2 = document.querySelector('#menu_btn_login2');
  var Process_SendingMsg = document.querySelector('#menu_btn_sendingMsg');

  Process_login.addEventListener('click', function () {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      var url_login = 'https://example.com/#login';
      chrome.permissions.request({ origins: [url_login] }, granted => {
        if (granted) {
          chrome.tabs.create({ url: url_login }, tab => {
            chrome.tabs.executeScript(tab.id, { file: 'scripts/login.js' });
          });
        } else {
          alert('You need to grant permission');
        }
      });
    });
  });

  Process_login2.addEventListener('click', function () {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
      chrome.tabs.executeScript(tabs[0].id, { file: 'scripts/login.js' });
    });
  });
permissions": 
        [
        "tabs",
        "tabCapture",
        "activeTab",
        "declarativeContent", 
        "storage"]
Eric
  • 357
  • 1
  • 4
  • 14
  • Replace `tab => function (tab) {` with `tab => {` – wOxxOm Feb 23 '21 at 12:49
  • @wOxxOm thx, i have updated the code but it does not work..... – Eric Feb 23 '21 at 12:57
  • Also replace `tabs[0].id` with `tab.id` in the same function. – wOxxOm Feb 23 '21 at 13:13
  • @wOxxOm still does not work. file: 'scripts/login.js' is not called. – Eric Feb 23 '21 at 14:14
  • What's in `permissions` of manifest.json? Also, these kind of problems should be trivial to resolve if you start using devtools debugger: set breakpoints inside callbacks and run the code to see/inspect what happens. Just in case, [How to open the correct devtools console to see output from an extension script?](https://stackoverflow.com/a/38920982) – wOxxOm Feb 23 '21 at 14:27
  • @wOxxOm i have updated permissions of manifest.json – Eric Feb 23 '21 at 14:52
  • Ah, that's because the newly opened tab wasn't activated by the user. See the description of [activeTab](https://developer.chrome.com/extensions/activeTab) permission. It won't work in this case so you will have to add `https://example.com/` to `permissions`. – wOxxOm Feb 23 '21 at 14:57

0 Answers0