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
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.
};
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.
This is the code you would put in test.js to import the functions into your file, and to be able to use it
Also, you may be able to import your script.js as a library.
In this case:
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!
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 themanifest.json
file. Currently, it seems to match only withhttps://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 themanifest.json
file for thetest.js
file as follows: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:Then, in the file injecting the content, you can call it like this: