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
    
  
  
    
  | document.addEventListener("DOMContentLoaded", function(event) { | |
| let title = document.querySelector("#firstHeading").textContent | |
| safari.extension.dispatchMessage(title); | |
| }); | 
  
    
      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
    
  
  
    
  | import SafariServices | |
| class SafariExtensionHandler: SFSafariExtensionHandler { | |
| override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) { | |
| SharedData.instance.messageName = messageName | |
| SharedData.instance.userInfo = userInfo | |
| } | |
| override func popoverViewController() -> SFSafariExtensionViewController { | 
  
    
      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
    
  
  
    
  | import SafariServices | |
| class SafariExtensionViewController: SFSafariExtensionViewController { | |
| @IBOutlet weak var label: NSTextField! | |
| override func viewWillAppear() { | |
| label.stringValue = SharedData.instance.messageName ?? "default" | |
| } | |
  
    
      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
    
  
  
    
  | import Cocoa | |
| import SafariServices.SFSafariApplication | |
| import SafariServices.SFSafariExtensionManager | |
| let defaults = UserDefaults.init(suiteName: "L27L4K8SQU.WikiReminder") | |
| class ViewController: NSViewController { | |
| @IBAction func refreshButtonPressed(_ sender: Any) { | |
| self.recentlyReadLabel.stringValue = defaults!.string(forKey: "WikipediaPageTitle") ?? "No value yet" | 
  
    
      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
    
  
  
    
  | document.addEventListener("contextmenu", handleContextMenu, false); | |
| function handleContextMenu(event) { | |
| var selectedText = window.getSelection().toString(); | |
| safari.extension.setContextMenuEventUserInfo(event, {"selectedText": selectedText}); | |
| } | 
  
    
      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
    
  
  
    
  | import SafariServices | |
| class SafariExtensionHandler: SFSafariExtensionHandler { | |
| override func contextMenuItemSelected(withCommand command: String, in page: SFSafariPage, userInfo: [String : Any]? = nil) { | |
| if command == "Search" { | |
| let redirectedURL = URL(string: "https://duckduckgo.com/?q=\(String(describing: userInfo!["selectdText"]))&t=h_&ia=web!")! | |
| page.getContainingTab { (tab) in | |
| tab.navigate(to: redirectedURL) | |
| } | 
  
    
      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
    
  
  
    
  | override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) { | |
| if messageName == "requiredMessage" { | |
| page.dispatchMessageToScript(withName: "logger") | |
| } | |
| } | 
  
    
      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
    
  
  
    
  | safari.self.addEventListener("message", function(event) { | |
| if (event.name == "logger") { | |
| console.log("messageRecieved is working just fine.") | |
| } | |
| }); | 
  
    
      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
    
  
  
    
  | // Add these functions to your SafariExtensionHandler subclass: | |
| // Given a Google, Yahoo, or Bing URL, this function will return the url encoding | |
| func getQueryStringParameter(url: String) -> String? { | |
| var param = "q" | |
| if url.contains("search.yahoo.com") { | |
| param = "p" | |
| } | |
| guard let url = URLComponents(string: url) else { return nil } | |
| let rawString = url.queryItems?.first(where: { $0.name == param })?.value | |
| let percentEncoding = rawString?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)?.replacingOccurrences(of: "&", with: "%26") |