skip to Main Content

I’m trying to write a Chrome extension that automates some tasks on my company’s internal website. The website uses .aspx files. I’m trying to call a function __doPostBack() from my extension but I get this error:

test.js:7 Uncaught ReferenceError: __doPostBack is not defined

test.js:

function clickLink() {
  __doPostBack('ctl00$Main$AssetSearchPanel$DialogContainers$GridContainers$ctl02$BtnName','')
}

clickLink()

manifest.json

{
  "manifest_version": 3,
  "name": "Containers v2",
  "description": "Base Level Extension",
  "version": "1.0",
  "content_scripts": [
    {
      "js": [
        "test.js"
      ],
      "matches": [
        "https://url.com"
      ]
    }
  ],
  "content_security_policy": {
    "extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self';"
  }
}

I tried circumventing calling this function by using inline scripting, but I couldn’t because of Chrome security policy.

I’m able to call the function from the Chrome DevTools console of the page.

2

Answers


  1. I am writing this answer on JavaScript knowledge, and this may not be the case on chrome extensions API or whatever it is called.

    Most of the time, when calling a function from a different file you would have to import or reference the file in your code.

    export function __doPostBack() {
    // Your code here
    

    };

    This is the code you would put in script.js to export your function, and to make it able to be accessible by other files.

    import { __doPostBack } from './script.js';
    

    This is the code you would put in test.js to import the functions into your file, and to be able to use it

    var test = new __doPostBack()
    

    Also, you may be able to import your script.js as a library.
    In this case:

    myLibrary.__doPostBack()
    

    References:
    import function from another JavaScript file

    Note: If there is an answer of someone actually knowing Google chrome extensions, please use their answer.

    Hope this helped you!

    Login or Signup to reply.
  2. To solve the problem, you can try the following steps:

    • In the test.js file, make sure to call the __doPostBack function correctly by ensuring that this function matches the URL specified in the manifest.json file. Currently, it seems to match only with https://url.com, so check that this URL is compatible with the target page.

    • If the issue persists, you can update the content_scripts section in the manifest.json file for the test.js file as follows:

      "content_scripts": [
        {
          "js": [
            "test.js"
          ],
          "matches": [
            "https://*.url.com/*"
          ]
        }
      ]
      

      This will match with all pages under url.com, including subdomains.

    • Due to Chrome’s security policy, instead of directly calling functions inside, you can communicate through a message. Add the following code to the test.js file:

      chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
          if (request.action == "callDoPostBack") {
            __doPostBack(request.params);
          }
        }
      );
      

      Then, in the file injecting the content, you can call it like this:

      chrome.runtime.sendMessage({action: "callDoPostBack", params: ['ctl00$Main$AssetSearchPanel$DialogContainers$GridContainers$ctl02$BtnName', '']});
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search