diff --git a/int/api/box.go b/int/api/box.go index a494afd..86a6d7a 100644 --- a/int/api/box.go +++ b/int/api/box.go @@ -4,6 +4,7 @@ import ( "bytes" _ "embed" "fmt" + "regexp" "github.com/massalabs/DeWeb/int/config" pkgConfig "github.com/massalabs/DeWeb/pkg/config" @@ -17,69 +18,48 @@ const ( //go:embed resources/massa_logomark.svg var massaLogomark []byte -// InjectHostedByMassaBox injects a "Hosted by Massa" box into the HTML content -func InjectHostedByMassaBox(content []byte, chainID uint64) []byte { +//go:embed resources/injectedStyle.css +var injectedStyle []byte + +//go:embed resources/massaBox.html +var massaBox []byte + +func InjectOnChainBox(content []byte, chainID uint64) []byte { + content = injectStyles(content) + content = injectHtmlBox(content, chainID) + + return content +} + +// InjectStyles injects the DeWeb label style into the HTML content +func injectStyles(content []byte) []byte { + styleHTML := fmt.Sprintf(` + + + `, injectedStyle) + + return bytes.Replace(content, []byte(""), []byte(styleHTML), 1) +} + +// InjectHtmlBox injects a "Hosted by Massa" box into the HTML content +func injectHtmlBox(content []byte, chainID uint64) []byte { chainName := getChainName(chainID) chainDocURL := getChainDocURL(chainID) - boxHTML := fmt.Sprintf(` - -
- -
- - - - -
Hosted on chain
-
-
-
- %s -
%s
-
-
- `, massaLogomark, chainDocURL, chainName, config.Version) - - // Insert the boxHTML before the closing tag - return bytes.Replace(content, []byte(""), []byte(boxHTML), 1) + boxHTML := fmt.Sprintf(string(massaBox), massaLogomark, chainDocURL, chainName, config.Version) + + bodyRegex := regexp.MustCompile(`(?i)]*>`) + + loc := bodyRegex.FindIndex(content) + if loc == nil { + return content + } + + result := append(content[:loc[1]], append([]byte(boxHTML), content[loc[1]:]...)...) + + return result } // getChainName returns the name of the chain based on the chainID diff --git a/int/api/middlewares.go b/int/api/middlewares.go index 411f69c..958daf3 100644 --- a/int/api/middlewares.go +++ b/int/api/middlewares.go @@ -150,7 +150,9 @@ func getWebsiteResource(network *msConfig.NetworkInfos, websiteAddress, resource if strings.HasPrefix(contentType, "text/html") { logger.Debugf("Injecting 'Hosted by Massa' box") - content = InjectHostedByMassaBox(content, network.ChainID) + + content = InjectOnChainBox(content, network.ChainID) + logger.Debugf("Injected 'Hosted by Massa' box\n%s", content) } diff --git a/int/api/resources/injectedStyle.css b/int/api/resources/injectedStyle.css new file mode 100644 index 0000000..182c1be --- /dev/null +++ b/int/api/resources/injectedStyle.css @@ -0,0 +1,73 @@ +@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Mono&display=swap"); + +.massa-logo { + width: 20px; + height: 20px; +} + +.massa-box { + display: none; + align-items: center; + justify-content: space-between; + + z-index: 100000; + position: relative; + + min-width: 100vw; + max-width: 100%; + + background: #ffffff; + + color: #010112; + opacity: 1; + padding: .25rem 1rem; +} + +.massa-box-disappeared { + display: none; +} + +.massa-box-content { + font-family: "IBM Plex Mono", monospace; + font-size: 14px; + display: flex; + flex-direction: row; + align-items: center; + width: 66%; + justify-content: space-between; + gap: 12px; + color: #010112; +} + +.massa-link, +.massa-logo-link { + text-decoration: none; + color: #010112; + cursor: pointer; +} + +.massa-on-chain-text { + text-wrap: nowrap; +} + +.hide-button { + background-color: transparent; + border: none; + font-size: 16px; + cursor: pointer; + position: relative; + color: #010112; +} + +.massa-flex-content { + display: flex; + align-items: center; + gap: 8px; +} + +/* Mobile Styles */ +@media (max-width: 880px) { + .massa-box-content { + justify-content: flex-start; + } +} diff --git a/int/api/resources/massaBox.html b/int/api/resources/massaBox.html new file mode 100644 index 0000000..f65d3af --- /dev/null +++ b/int/api/resources/massaBox.html @@ -0,0 +1,57 @@ + +
+
+ +
+ %s +
%s
+
+
+ +
+ + +