-
Notifications
You must be signed in to change notification settings - Fork 45
Added crossOrigin
, onLoad
, and onLoadFailed
config options
#675
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
base: main
Are you sure you want to change the base?
Changes from all commits
a10dcaa
32e3ad4
a58608e
e7954c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'react-use-intercom': minor | ||
--- | ||
|
||
Added crossOrigin, onLoad, and onLoadFailed config options |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('use crossOrigin', () => { | ||
it('should add attribute to script tag', () => { | ||
cy.visit('/useIntercomWithCrossOrigin'); | ||
|
||
cy.document() | ||
.get('head script') | ||
.should('have.attr', 'crossOrigin') | ||
.should('eq', 'anonymous'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// <reference types="cypress" /> | ||
|
||
describe('onLoad/onLoadFailed', () => { | ||
it('should call onLoad when successful', () => { | ||
cy.intercept('https://widget.intercom.io/widget/jcabc7e3', (request) => { | ||
request.continue((response) => { | ||
response.headers['cache-control'] = 'no-cache'; | ||
}); | ||
}); | ||
|
||
cy.visit('/useIntercomWithLoadCallbacks'); | ||
|
||
cy.get('[data-cy=call]').should(($p) => | ||
expect($p).to.have.text('onLoad was called!'), | ||
); | ||
}); | ||
|
||
it('should call onLoadFailed when not successful', () => { | ||
cy.intercept('https://widget.intercom.io/widget/jcabc7e3', { | ||
forceNetworkError: true, | ||
}); | ||
|
||
cy.visit('/useIntercomWithLoadCallbacks'); | ||
|
||
cy.get('[data-cy=call]').should(($p) => | ||
expect($p).to.have.text('onLoadFailed was called!'), | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { default as UseIntercomPage } from './useIntercom'; | ||
export { default as UseIntercomTourPage } from './useIntercomTour'; | ||
export { default as UseIntercomWithCrossOrigin } from './useIntercomWithCrossOrigin'; | ||
export { default as UseIntercomWithDelay } from './useIntercomWithDelay'; | ||
export { default as UseIntercomWithLoadCallbacks } from './useIntercomWithLoadCallbacks'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as React from 'react'; | ||
import { IntercomProvider } from 'react-use-intercom'; | ||
import styled from 'styled-components'; | ||
|
||
const Grid = styled.div` | ||
display: grid; | ||
grid-template-columns: repeat(1, 1fr); | ||
width: 100%; | ||
`; | ||
|
||
const Item = styled.div` | ||
display: grid; | ||
grid-template-rows: min-content; | ||
|
||
&::after { | ||
content: ''; | ||
margin: 2rem 0 1.5rem; | ||
border-bottom: 2px solid var(--grey); | ||
width: 100%; | ||
} | ||
`; | ||
|
||
const RawUseIntercomPage = () => { | ||
return ( | ||
<Grid> | ||
<Item> | ||
<p> | ||
Intercom will be initialized with{' '} | ||
<code>crossOrigin: "anonymous"</code> (and autobooted) | ||
</p> | ||
</Item> | ||
</Grid> | ||
); | ||
}; | ||
|
||
const UseIntercomWithCrossOriginPage = () => { | ||
return ( | ||
<IntercomProvider appId="jcabc7e3" autoBoot crossOrigin="anonymous"> | ||
<RawUseIntercomPage /> | ||
</IntercomProvider> | ||
); | ||
}; | ||
|
||
export default UseIntercomWithCrossOriginPage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useState } from 'react'; | ||
import * as React from 'react'; | ||
import { IntercomProvider } from 'react-use-intercom'; | ||
import styled from 'styled-components'; | ||
|
||
const Grid = styled.div` | ||
display: grid; | ||
grid-template-columns: repeat(1, 1fr); | ||
width: 100%; | ||
`; | ||
|
||
const Item = styled.div` | ||
display: grid; | ||
grid-template-rows: min-content; | ||
|
||
&::after { | ||
content: ''; | ||
margin: 2rem 0 1.5rem; | ||
border-bottom: 2px solid var(--grey); | ||
width: 100%; | ||
} | ||
`; | ||
|
||
const RawUseIntercomPage = ({ call }: { call: string | undefined }) => { | ||
return ( | ||
<Grid> | ||
<Item> | ||
<p> | ||
The Intercom Messenger script will be loaded and{' '} | ||
<code>onLoad/onLoadFailed</code> be called | ||
</p> | ||
<p data-cy="call">{call ?? 'Waiting…'}</p> | ||
</Item> | ||
</Grid> | ||
); | ||
}; | ||
|
||
const UseIntercomWithLoadCallbacks = () => { | ||
const [call, setCall] = useState<string>(); | ||
|
||
return ( | ||
<IntercomProvider | ||
appId="jcabc7e3" | ||
autoBoot | ||
onLoad={() => setCall('onLoad was called!')} | ||
onLoadFailed={() => setCall('onLoadFailed was called!')} | ||
> | ||
<RawUseIntercomPage call={call} /> | ||
</IntercomProvider> | ||
); | ||
}; | ||
|
||
export default UseIntercomWithLoadCallbacks; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,10 +4,19 @@ | |||||
* | ||||||
* @param appId - Intercom app id | ||||||
* @param [timeout=0] - Amount of milliseconds that the initialization should be delayed, defaults to 0 | ||||||
* @param [crossOrigin=undefined] - `crossOrigin` attribute value to use for the `<script>` tag, defaults to `undefined` | ||||||
* @param [onLoad=undefined] - Called when the Messenger script has been loaded successfully, defaults to `undefined`. | ||||||
* @param [onLoadFailed=undefined] - Called when the Messenger script has failed to load, defaults to `undefined`. | ||||||
* | ||||||
* @see {@link https://developers.intercom.com/installing-intercom/docs/basic-javascript} | ||||||
*/ | ||||||
const initialize = (appId: string, timeout = 0) => { | ||||||
const initialize = ( | ||||||
appId: string, | ||||||
timeout = 0, | ||||||
crossOrigin: string | undefined = undefined, | ||||||
onLoad: () => void = undefined, | ||||||
onLoadFailed: () => void = undefined, | ||||||
) => { | ||||||
var w = window; | ||||||
var ic = w.Intercom; | ||||||
if (typeof ic === 'function') { | ||||||
|
@@ -26,9 +35,21 @@ const initialize = (appId: string, timeout = 0) => { | |||||
var l = function () { | ||||||
setTimeout(function () { | ||||||
var s = d.createElement('script'); | ||||||
s.crossOrigin = crossOrigin; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
s.type = 'text/javascript'; | ||||||
s.async = true; | ||||||
s.src = 'https://widget.intercom.io/widget/' + appId; | ||||||
if (onLoad) { | ||||||
s.addEventListener('load', () => { | ||||||
onLoad(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be |
||||||
}); | ||||||
} | ||||||
if (onLoadFailed) { | ||||||
s.addEventListener('error', () => { | ||||||
// No need to pass any information from the ErrorEvent because it will contain no information about the error. | ||||||
onLoadFailed(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be also be |
||||||
}); | ||||||
} | ||||||
var x = d.getElementsByTagName('script')[0]; | ||||||
x.parentNode.insertBefore(s, x); | ||||||
}, timeout); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also copy and paste this in the other README?