Skip to content

Commit 5822c4b

Browse files
authored
Add watermark button for non-pro sandboxes (codesandbox#3907)
* Add watermark button * Add try...catch around the iframe code
1 parent fd21206 commit 5822c4b

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

packages/app/config/webpack.common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module.exports = {
138138
path.join(paths.sandboxSrc, 'index.js'),
139139
],
140140
'sandbox-startup': path.join(paths.sandboxSrc, 'startup.js'),
141+
'watermark-button': path.join(paths.src, 'watermark-button.js'),
141142
embed: [
142143
require.resolve('./polyfills'),
143144
path.join(paths.embedSrc, 'index.js'),
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const buttonStyles = `
2+
display: inline-flex;
3+
align-items: center;
4+
height: 32px;
5+
padding: 0 12px;
6+
font-size: 13px;
7+
font-weight: 500;
8+
color: white;
9+
background-color: rgb(21, 21, 21);
10+
cursor: pointer;
11+
border: 1px solid rgb(52,52,52);
12+
border-radius: 4px;
13+
text-decoration: none;
14+
font-family: system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Ubuntu,Droid Sans,Helvetica Neue,sans-serif;
15+
-webkit-font-smoothing: antialiased;
16+
-moz-osx-font-smoothing: antialiased;
17+
z-index: 99999999999;
18+
`;
19+
20+
const setButtonStyles = button => {
21+
button.setAttribute('style', buttonStyles);
22+
};
23+
24+
const setIframeStyle = iframe => {
25+
iframe.setAttribute(
26+
'style',
27+
`
28+
position: fixed;
29+
margin: 0;
30+
padding: 0;
31+
bottom: 16px;
32+
right: 16px;
33+
border: none;
34+
width: 118px;
35+
height: 36px;
36+
z-index: 9999999999999;
37+
`
38+
);
39+
40+
iframe.addEventListener('load', () => {
41+
iframe.contentDocument.body.setAttribute('style', `margin: 0;`);
42+
});
43+
};
44+
45+
function isStandalone() {
46+
if (typeof window === 'undefined') {
47+
return true;
48+
}
49+
50+
if (window.location && window.location.href.indexOf('?standalone') > -1) {
51+
return true;
52+
}
53+
54+
return !window.opener && window.parent === window;
55+
}
56+
57+
const createIframe = () => {
58+
if (!isStandalone()) {
59+
return;
60+
}
61+
62+
const iframe = document.createElement('iframe');
63+
iframe.setAttribute('id', 'open-sandbox');
64+
const link = document.createElement('a');
65+
setIframeStyle(iframe);
66+
67+
iframe.onload = () => {
68+
iframe.contentDocument.body.appendChild(link);
69+
setButtonStyles(link);
70+
link.innerText = 'Open Sandbox';
71+
72+
link.href =
73+
'https://codesandbox.io/s/' + document.location.host.split('.')[0];
74+
link.target = '_blank';
75+
link.rel = 'noopener noreferrer';
76+
77+
/**
78+
* Prevent others from trying to remove this button. If it's removed we just
79+
* readd it!
80+
*/
81+
const observer = new MutationObserver(() => {
82+
document.body.removeChild(iframe);
83+
observer.disconnect();
84+
createIframe();
85+
});
86+
87+
observer.observe(iframe, {
88+
attributes: true,
89+
childList: true,
90+
subtree: true,
91+
});
92+
};
93+
94+
document.body.appendChild(iframe);
95+
};
96+
window.addEventListener('load', () => {
97+
try {
98+
createIframe();
99+
} catch (e) {
100+
/* ignore */
101+
}
102+
});

0 commit comments

Comments
 (0)