Guest User

Word Of The Day Widget

a guest
Apr 21st, 2021
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. //This is meant for a small widget
  2. //You can change the bellow
  3. //Enter hex colours in double quotes for the colours.
  4. let backgroundColourLight = "#fff"
  5. let backgroundColourDark = "#000"
  6.  
  7. let titleColourLight = "#000"
  8. let titleColourDark = "#fff"
  9.  
  10. let textColourLight = "#000"
  11. let textColourDark = "#fff"
  12.  
  13. //Do not Change Below
  14. //Sets correct colours
  15. let backgroundColour = Color.dynamic(new Color(backgroundColourLight),new Color(backgroundColourDark))
  16. let titleColour = Color.dynamic(new Color(titleColourLight),new Color(titleColourDark))
  17. let textColour = Color.dynamic(new Color(textColourLight),new Color(textColourDark))
  18.  
  19. //Gets everything
  20. let url = "https://www.merriam-webster.com/word-of-the-day/"
  21. let req = new Request(url)
  22. let html = await req.loadString()
  23.  
  24. //Gets the Word
  25. let word = html.split("<title>Word of the Day: ")[1]
  26. word = word.split(" | Merriam-Webster</title>")[0]
  27. console.log(word)
  28.  
  29. //Gets the pronunciation and syllables
  30. let pronunciation = html.split('<span class="word-syllables">')[2]
  31. pronunciation = pronunciation.split("</span>")[0]
  32. console.log(pronunciation)
  33.  
  34. //Gets type, like verb
  35. let type = html.split('<span class="main-attr">')[1]
  36. type = type.split("</span>")[0]
  37. console.log(type)
  38.  
  39. //Gets the first definition. This might not work for all of the words because they are not all set up the same. But it will work for most words. The ➤ in the definition means that the following word is a synonym of the definition.
  40. let def = html.split("<h2>Definition</h2>")[1]
  41. def = def.split("<p>")[1]
  42.  
  43. def = def.split("</em>")
  44. if(def.length > 1) {
  45. def = def[1]
  46. def = def.split(" <strong>:</strong> ").join("")
  47. def = "old-fashioned word for " + def
  48. } else {
  49. def = def[0]
  50. }
  51. def = def.split("<strong>:</strong>").join("➤")
  52. def = def.split("</p>")[0]
  53. def = def.split(/<a[^>]*>|<\/a>/).join("")
  54. def = def.replace(/\<strong\>.*?\<\/strong\>/g)
  55.  
  56. console.log(def)
  57.  
  58. //Makes the widget
  59. let widget = new ListWidget()
  60. widget.backgroundColor = backgroundColour
  61. widget.url = "https://www.merriam-webster.com/word-of-the-day"
  62. let date = new Date()
  63. date.setHours(date.getHours() + 3)
  64. widget.refreshAfterDate = date
  65.  
  66. //Adds the word to the widget
  67. let title = widget.addText(word)
  68. title.font = Font.mediumRoundedSystemFont(20)
  69. title.textColor = titleColour
  70. title.centerAlignText()
  71. title.minimumScaleFactor = .7
  72. title.lineLimit = 1
  73. widget.addSpacer(10)
  74.  
  75. //Adds the type and pronunciation to the widget
  76. let text = widget.addText(type + " | " + pronunciation)
  77. text.font = Font.mediumRoundedSystemFont(15)
  78. text.textColor = textColour
  79. text.centerAlignText()
  80. text.minimumScaleFactor = .3
  81. text.lineLimit = 1
  82. widget.addSpacer(10)
  83.  
  84. //Adds the definition to the Widget
  85. def = widget.addText(def)
  86. def.font = Font.mediumRoundedSystemFont(13)
  87. def.textColor = textColour
  88. def.centerAlignText()
  89. def.minimumScaleFactor = .5
  90.  
  91. //Completes the script
  92. widget.presentSmall()
  93. Script.setWidget(widget)
  94. Script.complete()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment