Skip to content

Commit

Permalink
Merge pull request #291 from Darock-Studio/feature/safariext
Browse files Browse the repository at this point in the history
feat: Add Safari Extension
  • Loading branch information
WindowsMEMZ authored Mar 1, 2024
2 parents 51ed25e + 576e5b4 commit 6bc1166
Show file tree
Hide file tree
Showing 25 changed files with 359 additions and 13 deletions.
218 changes: 206 additions & 12 deletions DarockBili.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions MeowBili/MeowBiliApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ struct DarockBili_Watch_AppApp: App {
urlOpenVideoDetails.updateValue(String(kav[1]), forKey: String(kav[0]))
}
isUrlOpenVideoPresented = true
case "openbvid":
let bvid = spd[2]
urlOpenVideoDetails = ["Pic": "", "Title": "Loading...", "BV": String(bvid), "UP": "Loading...", "View": "1", "Danmaku": "1"]
isUrlOpenVideoPresented = true
default:
break
}
Expand Down
5 changes: 5 additions & 0 deletions MeowBili/Video/VideoDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,11 @@ struct VideoDetailView: View {
debugPrint("----------Prints from VideoDetailView.onAppear.*.requsetJSON(*/view)----------")
debugPrint(respJson)
if !CheckBApiError(from: respJson) { return }
videoDetails.updateValue(respJson["data"]["title"].string ?? "[加载失败]", forKey: "Title")
videoDetails.updateValue(respJson["data"]["pic"].string ?? "E", forKey: "Pic")
videoDetails.updateValue(String(respJson["data"]["stat"]["view"].int ?? 0), forKey: "View")
videoDetails.updateValue(String(respJson["data"]["stat"]["danmaku"].int ?? 0), forKey: "Danmaku")

owner = ["Name": respJson["data"]["owner"]["name"].string ?? "[加载失败]", "Face": respJson["data"]["owner"]["face"].string ?? "E", "ID": String(respJson["data"]["owner"]["mid"].int64 ?? -1)]
stat = ["Like": String(respJson["data"]["stat"]["like"].int ?? -1), "Coin": String(respJson["data"]["stat"]["coin"].int ?? -1), "Favorite": String(respJson["data"]["stat"]["favorite"].int ?? -1)]
videoDesc = respJson["data"]["desc"].string ?? "[加载失败]".replacingOccurrences(of: "\\n", with: "\n")
Expand Down
2 changes: 1 addition & 1 deletion MeowBili/Video/VideoPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ struct VideoPlayerView: View {
player.seek(to: CMTime(seconds: UserDefaults.standard.double(forKey: "\(videoBvid)\(videoCID)PlayTime"), preferredTimescale: 1))

if let coverData = try? Data(contentsOf: URL(string: videoDetails["Pic"] ?? "") ?? URL(string: "http://example.com")!) {
let cover = UIImage(data: coverData)!
let cover = UIImage(data: coverData) ?? UIImage()
NowPlayingExtension.setPlayingInfoTitle(videoDetails["Title"]!, artist: videoDetails["UP"]!, artwork: cover)
}

Expand Down
13 changes: 13 additions & 0 deletions SafariExtension/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.Safari.web-extension</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).SafariWebExtensionHandler</string>
</dict>
</dict>
</plist>
10 changes: 10 additions & 0 deletions SafariExtension/Resources/_locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extension_name": {
"message": "喵哩喵哩",
"description": "喵哩喵哩 Safari 扩展"
},
"extension_description": {
"message": "This is SafariExtension. You should tell us what your extension does here.",
"description": "Description of what the extension does."
}
}
1 change: 1 addition & 0 deletions SafariExtension/Resources/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions SafariExtension/Resources/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log("%cMeowBili Extension Here!", "color: #66CCFF;");
console.log("Current Link:", window.location.href);
var bvidSpdArr = window.location.href.split('/');
var bvid = bvidSpdArr[bvidSpdArr.length - 1];
window.location.href = "drkbili://openbvid/" + bvid;
Binary file added SafariExtension/Resources/images/icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/icon-256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/icon-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/icon-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/icon-96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/toolbar-icon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/toolbar-icon-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SafariExtension/Resources/images/toolbar-icon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions SafariExtension/Resources/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"manifest_version": 3,
"default_locale": "en",

"name": "__MSG_extension_name__",
"description": "__MSG_extension_description__",
"version": "1.0.0",

"icons": {
"48": "images/icon-48.png",
"96": "images/icon-96.png",
"128": "images/icon-128.png",
"256": "images/icon-256.png",
"512": "images/icon-512.png"
},

"background": {
"service_worker": "background.js"
},

"content_scripts": [{
"js": [ "content.js" ],
"matches": [ "*://*.bilibili.com/video/BV*" ]
}],

"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/toolbar-icon-16.png",
"19": "images/toolbar-icon-19.png",
"32": "images/toolbar-icon-32.png",
"38": "images/toolbar-icon-38.png",
"48": "images/toolbar-icon-48.png",
"72": "images/toolbar-icon-72.png"
}
},

"permissions": [ ]
}
14 changes: 14 additions & 0 deletions SafariExtension/Resources/popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
:root {
color-scheme: light dark;
}

body {
padding: 10px;

font-family: system-ui;
text-align: center;
}

@media (prefers-color-scheme: dark) {
/* Dark Mode styles go here. */
}
11 changes: 11 additions & 0 deletions SafariExtension/Resources/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="popup.css">
<script type="module" src="popup.js"></script>
</head>
<body>
<strong>喵哩喵哩 Safari 扩展已生效</strong>
</body>
</html>
1 change: 1 addition & 0 deletions SafariExtension/Resources/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

49 changes: 49 additions & 0 deletions SafariExtension/SafariWebExtensionHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
//
// SafariWebExtensionHandler.swift
// SafariExtension
//
// Created by memz233 on 2024/3/2.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the MeowBili open source project
//
// Copyright (c) 2024 Darock Studio and the MeowBili project authors
// Licensed under GNU General Public License v3
//
// See https://darock.top/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import SafariServices
import os.log

class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {

func beginRequest(with context: NSExtensionContext) {
let request = context.inputItems.first as? NSExtensionItem

let profile: UUID?
if #available(iOS 17.0, macOS 14.0, *) {
profile = request?.userInfo?[SFExtensionProfileKey] as? UUID
} else {
profile = request?.userInfo?["profile"] as? UUID
}

let message: Any?
if #available(iOS 17.0, macOS 14.0, *) {
message = request?.userInfo?[SFExtensionMessageKey]
} else {
message = request?.userInfo?["message"]
}

os_log(.default, "Received message from browser.runtime.sendNativeMessage: %@ (profile: %@)", String(describing: message), profile?.uuidString ?? "none")

let response = NSExtensionItem()
response.userInfo = [ SFExtensionMessageKey: [ "echo": message ] ]

context.completeRequest(returningItems: [ response ], completionHandler: nil)
}

}

0 comments on commit 6bc1166

Please sign in to comment.