diff --git a/intercom_flutter_web/CHANGELOG.md b/intercom_flutter_web/CHANGELOG.md index 904b548..9922958 100755 --- a/intercom_flutter_web/CHANGELOG.md +++ b/intercom_flutter_web/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.2 + +* Automatically Injected Intercom script, if it is not added. + ## 1.0.1 * Updated dependency `uuid: ^4.2.1`. diff --git a/intercom_flutter_web/README.md b/intercom_flutter_web/README.md index 5fdcd2c..c8e14ac 100755 --- a/intercom_flutter_web/README.md +++ b/intercom_flutter_web/README.md @@ -6,14 +6,14 @@ The web implementation of [`intercom_flutter`][1]. This package is already included as part of the `intercom_flutter` package dependency, and will be included when using `intercom_flutter` as normal. -But if you want to use this package as alone, first add the dependency `intercom_flutter_web` and then add the below script inside body tag in the index.html file located under web folder +But if you want to use this package as alone, add the dependency `intercom_flutter_web`. +You don't need to add Intercom script in the index.html file, it will be automatically injected. +But you can pre-define some Intercom settings, if you want (optional). ```html ``` #### Following functions are not yet supported on Web: diff --git a/intercom_flutter_web/lib/intercom_flutter_web.dart b/intercom_flutter_web/lib/intercom_flutter_web.dart index e821ad9..9e023a8 100644 --- a/intercom_flutter_web/lib/intercom_flutter_web.dart +++ b/intercom_flutter_web/lib/intercom_flutter_web.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:html'; import 'dart:js' as js; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; @@ -43,10 +44,24 @@ class IntercomFlutterWeb extends IntercomFlutterPlatform { String? androidApiKey, String? iosApiKey, }) async { - await js.context.callMethod('Intercom', [ - 'boot', - convertJsObjectToDartObject(updateIntercomSettings('app_id', appId)), - ]); + if (js.context['Intercom'] == null) { + // Intercom script is not added yet + // Inject it from here in the body + var script = ScriptElement(); + script.text = """ + window.intercomSettings = ${convertJsObjectToDartObject(updateIntercomSettings('app_id', "'$appId'"))}; + (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/' + '$appId';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s, x);};if(document.readyState==='complete'){l();}else if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})(); + """; + if (document.body != null) { + document.body!.append(script); + } + } else { + // boot the Intercom + await js.context.callMethod('Intercom', [ + 'boot', + convertJsObjectToDartObject(updateIntercomSettings('app_id', appId)), + ]); + } print("initialized"); } diff --git a/intercom_flutter_web/pubspec.yaml b/intercom_flutter_web/pubspec.yaml index daf05ca..a342723 100644 --- a/intercom_flutter_web/pubspec.yaml +++ b/intercom_flutter_web/pubspec.yaml @@ -1,6 +1,6 @@ name: intercom_flutter_web description: Web platform implementation of intercom_flutter -version: 1.0.1 +version: 1.0.2 homepage: https://github.com/v3rm0n/intercom_flutter flutter: