-
Notifications
You must be signed in to change notification settings - Fork 48
Add random “Tip of the Week” widget to homepage #148
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <section class="tip-of-the-week" aria-labelledby="tip-of-the-week-title"> | ||
| <div class="container"> | ||
| <div class="tip-of-the-week__card"> | ||
| <div class="tip-of-the-week__icon" aria-hidden="true"> | ||
| <i class="fas fa-lightbulb"></i> | ||
| </div> | ||
| <div class="tip-of-the-week__content"> | ||
| <span class="tip-of-the-week__eyebrow">MISP tip of the week</span> | ||
| <h2 id="tip-of-the-week-title">A useful tip from the MISP community</h2> | ||
| <p id="tip-of-the-week-text" class="tip-of-the-week__text" aria-live="polite">Loading a tip…</p> | ||
| <a id="tip-of-the-week-link" class="tip-of-the-week__link" href="https://github.com/cudeso/misp-tip-of-the-week" target="_blank" rel="noopener noreferrer" hidden> | ||
| Learn more <i class="fas fa-arrow-right" aria-hidden="true"></i> | ||
| </a> | ||
| </div> | ||
| <button id="tip-of-the-week-refresh" class="tip-of-the-week__refresh" type="button" aria-label="Show another MISP tip" title="Show another tip" hidden> | ||
| <i class="fas fa-random" aria-hidden="true"></i> | ||
| <span>Another tip</span> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </section> | ||
| <script src="{{ "js/tip-of-the-week.js" | relURL }}" defer></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| (function () { | ||
| "use strict"; | ||
|
|
||
| var sourceUrl = "https://raw.githubusercontent.com/cudeso/misp-tip-of-the-week/main/misp-tip-of-the-week.json"; | ||
| var repositoryUrl = "https://github.com/cudeso/misp-tip-of-the-week"; | ||
| var titleElement = document.getElementById("tip-of-the-week-title"); | ||
| var textElement = document.getElementById("tip-of-the-week-text"); | ||
| var linkElement = document.getElementById("tip-of-the-week-link"); | ||
| var refreshButton = document.getElementById("tip-of-the-week-refresh"); | ||
| var tips = []; | ||
| var currentIndex = -1; | ||
|
|
||
| if (!titleElement || !textElement || !linkElement || !refreshButton) { | ||
| return; | ||
| } | ||
|
|
||
| function firstString(object, keys) { | ||
| for (var i = 0; i < keys.length; i += 1) { | ||
| if (typeof object[keys[i]] === "string" && object[keys[i]].trim()) { | ||
| return object[keys[i]].trim(); | ||
| } | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| function normalizeTip(tip) { | ||
| if (typeof tip === "string") { | ||
| return { title: "MISP tip of the week", text: tip, url: "" }; | ||
| } | ||
|
|
||
| if (!tip || typeof tip !== "object") { | ||
| return null; | ||
| } | ||
|
|
||
| var text = firstString(tip, ["tip", "description", "content", "text", "body"]); | ||
| if (!text) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| title: firstString(tip, ["title", "name", "subject"]) || "MISP tip of the week", | ||
| text: text, | ||
| url: firstString(tip, ["url", "link", "reference", "source"]) | ||
| }; | ||
| } | ||
|
|
||
| function findEntries(data) { | ||
| if (Array.isArray(data)) { | ||
| return data; | ||
| } | ||
|
|
||
| if (!data || typeof data !== "object") { | ||
| return []; | ||
| } | ||
|
|
||
| var preferredEntries = data.tips || data.items || data.entries; | ||
| if (Array.isArray(preferredEntries)) { | ||
| return preferredEntries; | ||
| } | ||
|
|
||
| var keys = Object.keys(data); | ||
| for (var i = 0; i < keys.length; i += 1) { | ||
| if (Array.isArray(data[keys[i]])) { | ||
| return data[keys[i]]; | ||
| } | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| function safeHttpUrl(value) { | ||
| if (!value) { | ||
| return ""; | ||
| } | ||
|
|
||
| try { | ||
| var url = new URL(value, repositoryUrl); | ||
| return url.protocol === "https:" || url.protocol === "http:" ? url.href : ""; | ||
| } catch (error) { | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| function showRandomTip() { | ||
| var nextIndex; | ||
|
|
||
| if (!tips.length) { | ||
| return; | ||
| } | ||
|
|
||
| do { | ||
| nextIndex = Math.floor(Math.random() * tips.length); | ||
| } while (tips.length > 1 && nextIndex === currentIndex); | ||
|
|
||
| currentIndex = nextIndex; | ||
| titleElement.textContent = tips[currentIndex].title; | ||
| textElement.textContent = tips[currentIndex].text; | ||
|
|
||
| var tipUrl = safeHttpUrl(tips[currentIndex].url); | ||
| linkElement.href = tipUrl || repositoryUrl; | ||
| linkElement.hidden = !tipUrl; | ||
| } | ||
|
|
||
| refreshButton.addEventListener("click", showRandomTip); | ||
|
|
||
| fetch(sourceUrl, { headers: { "Accept": "application/json" } }) | ||
| .then(function (response) { | ||
| if (!response.ok) { | ||
| throw new Error("Tip request failed with status " + response.status); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .then(function (data) { | ||
| tips = findEntries(data).map(normalizeTip).filter(Boolean); | ||
|
|
||
| if (!tips.length) { | ||
| throw new Error("The tip feed did not contain any supported tips"); | ||
| } | ||
|
|
||
| showRandomTip(); | ||
| refreshButton.hidden = tips.length < 2; | ||
| }) | ||
| .catch(function () { | ||
| titleElement.textContent = "MISP tip of the week"; | ||
| textElement.textContent = "The community tip could not be loaded right now."; | ||
| linkElement.href = repositoryUrl; | ||
| linkElement.textContent = "Browse all tips"; | ||
| linkElement.hidden = false; | ||
| }); | ||
| }()); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In browsers or embedded webviews without the Fetch API, this call throws a synchronous
ReferenceError, so the promise.catch()is never reached and the widget remains stuck on “Loading a tip…” rather than displaying its graceful fallback. Feature-detectwindow.fetchand invoke the same fallback path when it is unavailable.Useful? React with 👍 / 👎.