Skip to content

Instantly share code, notes, and snippets.

@Rene-Sackers
Last active February 12, 2017 09:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rene-Sackers/e86e0a2a19c5d81a51a974d5c5f90f1b to your computer and use it in GitHub Desktop.
Save Rene-Sackers/e86e0a2a19c5d81a51a974d5c5f90f1b to your computer and use it in GitHub Desktop.
const BrowserLoadTime = 1000;
class CefHelper {
public static eventHandlers: { [key: string]: (args: any[]) => void } = {};
public get IsOpen() { return this._isOpen; }
private _browserControl: GTANetwork.GUI.Browser;
private _pagePath: string;
private _isCreated = false;
private _isOpen = false;
private _local = true;
private _eventQueue = new Array<(browser: GTANetwork.GUI.Browser) => void>();
private _creationTime = 0;
constructor(pagePath: string, local: boolean = true) {
this._pagePath = pagePath;
this._local = local;
API.onResourceStop.connect(this.onResourceStop);
API.onUpdate.connect(this.onUpdate);
}
private onResourceStop = () => {
this.destroy();
};
private onUpdate = () => {
if (!this._isOpen) return;
if (this._eventQueue.length > 0 && this._creationTime > 0 && API.getGlobalTime() - this._creationTime >= BrowserLoadTime)
{
this._eventQueue.shift()(this._browserControl);
}
API.disableAllControlsThisFrame();
};
private create = () => {
if (this._isCreated) return;
var resolution = API.getScreenResolution();
this._browserControl = API.createCefBrowser(resolution.Width, resolution.Height, this._local);
API.waitUntilCefBrowserInit(this._browserControl);
API.setCefBrowserPosition(this._browserControl, 0, 0);
API.loadPageCefBrowser(this._browserControl, this._pagePath)
this._creationTime = API.getGlobalTime();
this._isCreated = true;
};
private destroy = () => {
if (!this._isCreated || typeof (this._browserControl) == "undefined" || this._browserControl == null) return;
this.hide();
API.sendChatMessage("destroy");
API.destroyCefBrowser(this._browserControl);
this._browserControl.Dispose();
this._browserControl = null;
this._isCreated = false;
}
public show = () => {
if (this._isOpen) return;
this.create();
this._isOpen = true;
API.setCefBrowserHeadless(this._browserControl, false);
API.showCursor(true);
API.setCanOpenChat(false);
};
public hide = () => {
if (!this._isOpen) return;
this._isOpen = false;
API.setCanOpenChat(true);
API.showCursor(false);
API.setCefBrowserHeadless(this._browserControl, true);
};
public callMethod = (caller: (browser: GTANetwork.GUI.Browser) => void) => {
this._eventQueue.push(caller);
};
public static browserCallback = (eventName: string, ...args: any[]) => {
var handler = CefHelper.eventHandlers[eventName];
if (typeof (handler) == undefined || handler == null) {
API.sendChatMessage("~r~No handler for browser event: " + eventName);
return;
}
handler(args);
}
}
@Alf21
Copy link

Alf21 commented Feb 12, 2017

Nice gj, i will try it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment