Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically Inject Intercom script #401

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions intercom_flutter_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
8 changes: 4 additions & 4 deletions intercom_flutter_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script>
window.intercomSettings = {
hide_default_launcher: true, // set this to false, if you want to show the default launcher
hide_default_launcher: true, // hide the launcher
};
(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/';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);}}})();

</script>
```
#### Following functions are not yet supported on Web:
Expand Down
23 changes: 19 additions & 4 deletions intercom_flutter_web/lib/intercom_flutter_web.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion intercom_flutter_web/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Loading