bitwarden-estensione-browser/src/popup/services/popup-utils.service.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.6 KiB
TypeScript
Raw Normal View History

import { Injectable } from "@angular/core";
2018-12-03 17:23:14 +01:00
import { BrowserApi } from "../../browser/browserApi";
@Injectable()
export class PopupUtilsService {
constructor(private privateMode: boolean = false) {}
2018-12-03 17:23:14 +01:00
inSidebar(win: Window): boolean {
return win.location.search !== "" && win.location.search.indexOf("uilocation=sidebar") > -1;
}
inTab(win: Window): boolean {
return win.location.search !== "" && win.location.search.indexOf("uilocation=tab") > -1;
}
inPopout(win: Window): boolean {
return win.location.search !== "" && win.location.search.indexOf("uilocation=popout") > -1;
}
inPopup(win: Window): boolean {
return (
win.location.search === "" ||
win.location.search.indexOf("uilocation=") === -1 ||
win.location.search.indexOf("uilocation=popup") > -1
);
}
2018-04-09 20:17:58 +02:00
inPrivateMode(): boolean {
return this.privateMode;
}
2022-02-24 18:14:04 +01:00
getContentScrollY(win: Window, scrollingContainer = "content"): number {
const content = win.document.getElementsByTagName(scrollingContainer)[0];
2018-04-09 20:17:58 +02:00
return content.scrollTop;
}
2022-02-24 18:14:04 +01:00
setContentScrollY(win: Window, scrollY: number, scrollingContainer = "content"): void {
2018-04-09 20:17:58 +02:00
if (scrollY != null) {
const content = win.document.getElementsByTagName(scrollingContainer)[0];
2018-04-09 20:17:58 +02:00
content.scrollTop = scrollY;
}
2021-12-21 15:43:35 +01:00
}
2018-12-03 17:23:14 +01:00
popOut(win: Window, href: string = null): void {
if (href === null) {
href = win.location.href;
}
2018-12-03 17:23:14 +01:00
if (typeof chrome !== "undefined" && chrome.windows && chrome.windows.create) {
if (href.indexOf("?uilocation=") > -1) {
href = href
.replace("uilocation=popup", "uilocation=popout")
.replace("uilocation=tab", "uilocation=popout")
.replace("uilocation=sidebar", "uilocation=popout");
} else {
const hrefParts = href.split("#");
href =
hrefParts[0] + "?uilocation=popout" + (hrefParts.length > 0 ? "#" + hrefParts[1] : "");
}
const bodyRect = document.querySelector("body").getBoundingClientRect();
chrome.windows.create({
url: href,
type: "popup",
width: Math.round(bodyRect.width ? bodyRect.width + 60 : 375),
height: Math.round(bodyRect.height || 600),
});
if (this.inPopup(win)) {
BrowserApi.closePopup(win);
}
} else if (typeof chrome !== "undefined" && chrome.tabs && chrome.tabs.create) {
href = href
.replace("uilocation=popup", "uilocation=tab")
.replace("uilocation=popout", "uilocation=tab")
.replace("uilocation=sidebar", "uilocation=tab");
chrome.tabs.create({
url: href,
});
}
2021-12-21 15:43:35 +01:00
}
}