1. The Problem
I'm trying to access a global variable from a website — random OGS Example — through TypeScript in order to get data from the current board game.
I'm pretty new to TS so I'm not really sure how I would type this variable either. The global variable itself was originally created in TS actually, but it has a bazillion methods and properties, since there are so many functionalities. So, if I'm going to type it myself, I would rather only type what I need, if that's possible, or somehow import it from a package somewhere — I don't think the author has published it publicly anywhere.
In parallel to all this, there's also the question of whether or not global variables are available by default to browser extensions, which might not be the case.
2. What I've Been Trying
So, what I've been trying (I've tried a million other variations...) to do is something like* — inside content.ts, which is run when inside the OGS domain —:
interface Goban {
bounded_height: number;
}
declare var goban: Goban;
goban = window["global_goban"];
console.log(goban.bounded_height.toString());
The error I usually get is:
Uncaught ReferenceError: goban is not defined
So what am I missing? Do I have to create a declaration file (.d.ts)? What is the proper way of dealing with this sort of import of a global variable from 3rd party code?
*: Depending on how you're trying to deal with the window object, you might have to add "suppressImplicitAnyIndexErrors": true to your tsconfig.json in order to have TS not complaining statically.
3. How to Fully Reproduce My Problem
For those unfamiliar with how to develop browser extensions, the simplest way to start, in TS, is to:
- Create a simple
manifest.json, for example:{ "manifest_version": 2, "name": "Name", "version": "0.0.2", "description": "Description.", "content_scripts": [ { "matches": ["*://*.online-go.com/*"], "js": ["content.js"] } ] } - Create a
content.tsfile, with the code from the previous section. - Compile to JS with
tsc. - Import the
manifest.jsonand thecontent.jsinto a browser as an unpacked browser extension. - Load a random OGS game and inspect it.