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

fix: Trigger url evaluation requiring CORS access #29304

Merged
merged 13 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/strong-fans-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/livechat': patch
---

Fixes Livechat page-url triggers requiring CORS to access the parent url
3 changes: 3 additions & 0 deletions packages/livechat/src/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ const api = {
store.setState({ minimized: false });
parentCall('openWidget');
},
setParentUrl(parentUrl) {
store.setState({ parentUrl });
},
};

const onNewMessage = (event) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/livechat/src/lib/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Triggers {
token,
firedTriggers = [],
config: { triggers },
parentUrl,
} = store.state;
Livechat.credentials.token = token;

Expand All @@ -88,6 +89,7 @@ class Triggers {
}

this._started = true;
this._parentUrl = parentUrl;
this._triggers = [...triggers];

firedTriggers.forEach((triggerId) => {
Expand Down Expand Up @@ -166,7 +168,7 @@ class Triggers {
switch (condition.name) {
case 'page-url':
const hrefRegExp = new RegExp(condition.value, 'g');
if (hrefRegExp.test(window.parent.location.href)) {
if (hrefRegExp.test(this._parentUrl)) {
this.fire(trigger);
}
break;
Expand Down
9 changes: 9 additions & 0 deletions packages/livechat/src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const createWidget = (url) => {
smallScreen = matches;
updateWidgetStyle(widget.dataset.state === 'opened');
callHook('setExpanded', smallScreen);
callHook('setParentUrl', window.location.url);
aleksandernsilva marked this conversation as resolved.
Show resolved Hide resolved
};

const mediaQueryList = window.matchMedia('screen and (max-device-width: 480px)');
Expand Down Expand Up @@ -318,6 +319,10 @@ function minimizeWidget() {
callHook('minimizeWidget');
}

function setParentUrl(url) {
callHook('setParentUrl', url);
}

function initialize(params) {
for (const method in params) {
if (!params.hasOwnProperty(method)) {
Expand Down Expand Up @@ -367,6 +372,9 @@ function initialize(params) {
case 'agent':
setAgent(params[method]);
continue;
case 'parentUrl':
setParentUrl(params[method]);
continue;
default:
continue;
}
Expand Down Expand Up @@ -457,6 +465,7 @@ window.RocketChat.livechat = {
minimizeWidget,
setBusinessUnit,
clearBusinessUnit,
setParentUrl,

// callbacks
onChatMaximized(fn) {
Expand Down