Skip to content

Commit

Permalink
Merge pull request #17 from Quorafind/feat/support-copy-video-link-bi…
Browse files Browse the repository at this point in the history
…libili

feat: support bilibili video link
  • Loading branch information
Quorafind authored Nov 21, 2022
2 parents 22c2adb + bb560d6 commit 9a4f0de
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "another-web-browser",
"name": "Another Web Browser",
"version": "0.1.8",
"version": "0.1.9",
"minAppVersion": "1.0.0",
"description": "Lets you browse the web within Obsidian with some neat features.",
"author": "Dion Tryban (Trikzon) && Boninall",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "another-web-browser",
"version": "0.1.8",
"version": "0.1.9",
"description": "Lets you browse the web within Obsidian with some neat features.",
"main": "main.js",
"scripts": {
Expand Down
54 changes: 49 additions & 5 deletions src/web_browser_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ export class WebBrowserView extends ItemView {
static spawnWebBrowserView(newLeaf: boolean, state: WebBrowserViewState) {
const isOpenInSameTab = app.plugins.getPlugin("another-web-browser").settings.openInSameTab
if (isOpenInSameTab) {
const leafId = app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID).length ? localStorage.getItem("web-browser-leaf-id") : app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID)[0]?.id
const leafId = app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID).length ? localStorage.getItem("web-browser-leaf-id") : app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID)[0]?.id;
if (!leafId) {
const activeView = app.workspace.getActiveViewOfType(MarkdownView)?.leaf
if (!activeView) return;
const leaf = app.workspace.createLeafBySplit(activeView) as WorkspaceLeaf
// Check if current leaf is empty view or markdown view.
let activeViewLeaf: WorkspaceLeaf | undefined;
activeViewLeaf = app.workspace.getActiveViewOfType(MarkdownView)?.leaf
if (!activeViewLeaf) activeViewLeaf = app.workspace.getActiveViewOfType(ItemView)?.getViewType() === "empty" ? app.workspace.getActiveViewOfType(ItemView)?.leaf : undefined
if (!activeViewLeaf) return;

const leaf = app.workspace.getActiveViewOfType(ItemView)?.getViewType() === "empty" ? activeViewLeaf : app.workspace.createLeafBySplit(activeViewLeaf) as WorkspaceLeaf;
localStorage.setItem("web-browser-leaf-id", leaf.id)
leaf.setViewState({ type: WEB_BROWSER_VIEW_ID, active: true, state })
leaf.setPinned(true);
Expand All @@ -38,6 +42,18 @@ export class WebBrowserView extends ItemView {
app.workspace.getLeafById(leafId).setViewState({ type: WEB_BROWSER_VIEW_ID, active: true, state });
}
} else {
if (state.url.contains("bilibili")) {
for (let i = 0; i < app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID).length; i++) {
if (app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID)[i].getViewState().state.url.split('?t=')[0] === state.url.split('?t=')[0]) {
app.workspace.getLeavesOfType(WEB_BROWSER_VIEW_ID)[i].setViewState({
type: WEB_BROWSER_VIEW_ID,
active: true,
state
});
return;
}
}
}
app.workspace.getLeaf(newLeaf).setViewState({ type: WEB_BROWSER_VIEW_ID, active: true, state });
}
}
Expand Down Expand Up @@ -135,7 +151,7 @@ export class WebBrowserView extends ItemView {
}));
const highlightFormat = this.plugin.settings.highlightFormat;
menu.append(new MenuItem({
label: 'Copy Highlight Link', click: function (this: any) {
label: 'Copy Highlight Link', click: function () {
try {
// eslint-disable-next-line no-useless-escape
const linkToHighlight = params.pageURL.replace(/\#\:\~\:text\=(.*)/g, "") + "#:~:text=" + encodeURIComponent(params.selectionText);
Expand All @@ -162,6 +178,34 @@ export class WebBrowserView extends ItemView {
menu.popup(webContents);
}

if (params.pageURL?.contains("bilibili")) {
menu.append(new MenuItem({
label: 'Copy Video Time', click: function () {
try {
webContents.executeJavaScript(`
var time = document.querySelectorAll('.bpx-player-ctrl-time-current')[0].innerHTML;
var timeYMSArr=time.split(':');
var joinTimeStr='00h00m00s';
if(timeYMSArr.length===3){
joinTimeStr=timeYMSArr[0]+'h'+timeYMSArr[1]+'m'+timeYMSArr[2]+'s';
}else if(timeYMSArr.length===2){
joinTimeStr=timeYMSArr[0]+'m'+timeYMSArr[1]+'s';
}
var timeStr= "";
timeStr = window.location.href.split('?')[0]+'?t=' + joinTimeStr;
`, true).then((result: any) => {
clipboard.writeText("[" + result.split('?t=')[1] + "](" + result + ")"); // Will be the JSON object from the fetch call
});
console.log('Page URL copied to clipboard');
} catch (err) {
console.error('Failed to copy: ', err);
}
}
}));

menu.popup(webContents);
}

// Should use this method to prevent default copy+c
// The default context menu is related to the shadow root that in the webview tag
// So it is not possible to preventDefault because it cannot be accessed.
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"0.1.5": "1.0.0",
"0.1.6": "1.0.0",
"0.1.7": "1.0.0",
"0.1.8": "1.0.0"
"0.1.8": "1.0.0",
"0.1.9": "1.0.0"
}

0 comments on commit 9a4f0de

Please sign in to comment.