mirror of https://github.com/jumpserver/jumpserver
Merge pull request #10966 from jumpserver/pr@dev@perf_chrome_plugins
perf: 优化 chrome 插件pull/10979/head
commit
d99a3455cd
|
@ -1,23 +1,54 @@
|
||||||
// background.js
|
// background.js
|
||||||
|
|
||||||
|
const tabs = []
|
||||||
|
const debug = console.log
|
||||||
|
|
||||||
// 监听标签页的创建事件
|
// 监听标签页的创建事件
|
||||||
chrome.tabs.onCreated.addListener(function (tab) {
|
chrome.tabs.onCreated.addListener(function (tab) {
|
||||||
// 获取当前窗口的所有标签页
|
// 获取当前窗口的所有标签页
|
||||||
chrome.tabs.query({currentWindow: true}, function (tabs) {
|
debug('New tab add, tabs : ', tabs)
|
||||||
// 如果当前窗口的标签页数量大于1,则关闭新创建的标签页
|
tabs.push(tab)
|
||||||
if (tabs.length > 1) {
|
|
||||||
chrome.tabs.remove(tab.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 监听窗口的创建事件
|
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
||||||
chrome.windows.onCreated.addListener(function (window) {
|
debug('Tab changed xx: ', tabId, changeInfo, tab)
|
||||||
// 获取当前所有窗口
|
if (changeInfo.status !== 'loading') {
|
||||||
chrome.windows.getAll(function (windows) {
|
return
|
||||||
// 如果当前窗口数量大于1,则关闭新创建的窗口
|
|
||||||
if (windows.length > 1) {
|
|
||||||
chrome.windows.remove(window.id);
|
|
||||||
}
|
}
|
||||||
});
|
const tabFind = tabs.findIndex(t => t.id === tabId)
|
||||||
});
|
if (tabFind === -1) {
|
||||||
|
debug('Tab not found: ', tabId, tabs)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Object.assign(tabs[tabFind], tab)
|
||||||
|
|
||||||
|
const blockUrls = ['chrome://newtab/']
|
||||||
|
if (!tab.url || blockUrls.includes(tab.url) || tab.url.startsWith('chrome://')) {
|
||||||
|
debug('Blocked url, destroy: ', tab.url)
|
||||||
|
chrome.tabs.remove(tabId);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第一个 tab 不做限制
|
||||||
|
// 修改初始 tab 的状态,因为第一个 tab 没有地址栏,可以允许它自由跳转
|
||||||
|
if (tabs.length === 1) {
|
||||||
|
debug('First tab, pass')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const firstUrl = tabs[0].url
|
||||||
|
const curUrl = tab.url
|
||||||
|
if (!firstUrl.startsWith('http') || !curUrl.startsWith('http')) {
|
||||||
|
debug('First tab url empty, or current empty, pass ', firstUrl, curUrl)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const firstTabHost = new URL(firstUrl).host
|
||||||
|
const curHost = new URL(curUrl).host
|
||||||
|
const firstDomain = firstTabHost.split('.').slice(-2).join('.')
|
||||||
|
const curDomain = curHost.split('.').slice(-2).join('.')
|
||||||
|
if (firstDomain !== curDomain) {
|
||||||
|
debug('Not same domain, destroy: ', firstTabHost, ' current: ', curHost)
|
||||||
|
chrome.tabs.remove(tabId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
// content_script.js
|
// content_script.js
|
||||||
|
|
||||||
|
const debug = console.log
|
||||||
|
|
||||||
// 创建一个 Mutation Observer 实例
|
// 创建一个 Mutation Observer 实例
|
||||||
const observer = new MutationObserver(function (mutationsList) {
|
const observer = new MutationObserver(function (mutationsList) {
|
||||||
// 遍历每个发生变化的 mutation
|
// 遍历每个发生变化的 mutation
|
||||||
|
@ -10,7 +12,7 @@ const observer = new MutationObserver(function (mutationsList) {
|
||||||
const links = document.getElementsByTagName('a');
|
const links = document.getElementsByTagName('a');
|
||||||
|
|
||||||
// 遍历 <a> 标签元素并修改链接属性
|
// 遍历 <a> 标签元素并修改链接属性
|
||||||
console.log("开始替换标签")
|
debug("开始替换标签")
|
||||||
for (let i = 0; i < links.length; i++) {
|
for (let i = 0; i < links.length; i++) {
|
||||||
links[i].target = '_self'; // 将 target 属性设置为 _self,当前窗口打开
|
links[i].target = '_self'; // 将 target 属性设置为 _self,当前窗口打开
|
||||||
}
|
}
|
||||||
|
@ -27,43 +29,26 @@ const observer = new MutationObserver(function (mutationsList) {
|
||||||
// 开始观察 document.body 的子节点变化
|
// 开始观察 document.body 的子节点变化
|
||||||
observer.observe(document.body, {childList: true, subtree: true});
|
observer.observe(document.body, {childList: true, subtree: true});
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(
|
|
||||||
function (request, sender, sendResponse) {
|
|
||||||
console.log(request.url);
|
|
||||||
$("iframe").attr("src", request.url);
|
|
||||||
sendResponse({farewell: "goodbye"});
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
document.addEventListener("contextmenu", function (event) {
|
document.addEventListener("contextmenu", function (event) {
|
||||||
console.log('On context')
|
debug('On context')
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
var AllowedKeys = ['P', 'F', 'C', 'V']
|
const AllowedKeys = ['P', 'F', 'C', 'V']
|
||||||
window.addEventListener("keydown", function (e) {
|
window.addEventListener("keydown", function (e) {
|
||||||
if (e.key === "F12" || (e.ctrlKey && !AllowedKeys.includes(e.key.toUpperCase()))) {
|
if (e.key === "F12" || (e.ctrlKey && !AllowedKeys.includes(e.key.toUpperCase()))) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
console.log('Press key: ', e.ctrlKey ? 'Ctrl' : '', e.shiftKey ? ' Shift' : '', e.key)
|
debug('Press key: ', e.ctrlKey ? 'Ctrl' : '', e.shiftKey ? ' Shift' : '', e.key)
|
||||||
}
|
}
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
// 保存原始的 window.open 函数引用
|
|
||||||
var originalOpen = window.open;
|
|
||||||
|
|
||||||
// 修改 window.open 函数
|
// 修改 window.open 函数
|
||||||
window.open = function (url, target, features) {
|
window.open = function (url, target, features) {
|
||||||
// 将 target 强制设置为 "_self",使得新页面在当前标签页中打开
|
// 将 target 强制设置为 "_self",使得新页面在当前标签页中打开
|
||||||
target = "_self";
|
target = "_self";
|
||||||
|
debug('Open url: ', url, target, features)
|
||||||
// 修改当前页面的 URL
|
|
||||||
location.href = url;
|
|
||||||
|
|
||||||
// 调用原始的 window.open 函数
|
// 调用原始的 window.open 函数
|
||||||
return originalOpen.call(this, url, target, features);
|
window.href = url
|
||||||
|
// return originalOpen.call(this, url, target, features);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
|
|
||||||
});
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
name: chrome
|
name: chrome
|
||||||
display_name: "{{ 'Chrome Browser' | trans }}"
|
display_name: "{{ 'Chrome Browser' | trans }}"
|
||||||
version: 0.5
|
version: 0.6
|
||||||
comment: "{{ 'Chrome Browser Open URL Page Address' | trans }}"
|
comment: "{{ 'Chrome Browser Open URL Page Address' | trans }}"
|
||||||
author: JumpServer Team
|
author: JumpServer Team
|
||||||
exec_type: python
|
exec_type: python
|
||||||
|
|
Loading…
Reference in New Issue