chrome.proxy.settings.set(
{ value: { "mode": "system" }, scope: 'regular' },
function () { });
// 在页面加载完成后注入JavaScript
// chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// if (changeInfo.status == 'complete') {
// alert(tabId)
// chrome.tabs.executeScript(tabId, {
// code: `
// // 在页面中创建按钮
// const button = document.createElement('button');
// button.innerHTML = '点击我修改页面内容';
// // 当按钮被点击时,修改页面内容
// button.addEventListener('click', function() {
// // 在这里添加修改页面内容的代码
// });
// // 将按钮添加到页面中
// document.body.appendChild(button);
// `});
// }
// });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (JSON.stringify(changeInfo.title).indexOf("_proxy_") != -1) {
title = changeInfo.title
if (title.indexOf("_proxy_") != -1) {
title = title.split("_proxy_")
let _title = title[1].split(":")
chrome.proxy.settings.set(
{ value: { "mode": "fixed_servers", "rules": { "bypassList": ["<local>"], "singleProxy": { "scheme": _title[0], "host": _title[1], "port": parseInt(_title[2]) } } }, scope: 'regular' },
function () { });
// alert(JSON.stringify(tab) + tab.id + "document.title = '" + title[0] + "'")
chrome.tabs.executeScript(tab.id, { code: "document.title = '" + title[0] + "'" });
}
}
});
document.addEventListener('DOMContentLoaded', () => {
setInterval(function () {
chrome.tabs.query({ 'active': true, 'windowId': chrome.windows.WINDOW_ID_CURRENT },
function (tabs) {
let tab = tabs[0], url = new URL(tab.url),
title = tab.title
if (title.indexOf("_proxy_") != -1) {
title = title.split("_proxy_")
let _title = title[1].split(":")
chrome.proxy.settings.set(
{ value: { "mode": "fixed_servers", "rules": { "bypassList": ["<local>"], "singleProxy": { "scheme": _title[0], "host": _title[1], "port": parseInt(_title[2]) } } }, scope: 'regular' },
function () { });
// alert(JSON.stringify(tab) + tab.id + "document.title = '" + title[0] + "'")
chrome.tabs.executeScript(tab.id, { code: "document.title = '" + title[0] + "'" });
}
})
}, 1000)
})
精简之后【原理:background.js自动加载JS
利用chrome.proxy设置动态IP特性,chrome.tabs.onUpdated监控changeInfo标题事件 判断是否需要切换 标题切换格式为 原标题_proxy_(http:https:socks5:socks4):host:port
例如 无感切换_http:127.0.0.1:9999】
如何从扩展程序中检测Google Chrome中的页面标题更改?
chrome.proxy.settings.set( { value: { "mode": "system" }, scope: 'regular' }, function () { });
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (JSON.stringify(changeInfo.title).indexOf("_proxy_") != -1) {
title = changeInfo.title
if (title.indexOf("_proxy_") != -1) {
title = title.split("_proxy_")
let _title = title[1].split(":")
chrome.proxy.settings.set(
{ value: { "mode": "fixed_servers", "rules": { "bypassList": ["<local>"], "singleProxy": { "scheme": _title[0], "host": _title[1], "port": parseInt(_title[2]) } } }, scope: 'regular' },
function () { });
// alert(JSON.stringify(tab) + tab.id + "document.title = '" + title[0] + "'")
chrome.tabs.executeScript(tab.id, { code: "document.title = '" + title[0] + "'" });
}
}
});