for Firefox

This commit is contained in:
changjin wei(魏昌进) 2025-02-01 23:57:42 +08:00
parent 809f66e01e
commit 153395c475
5 changed files with 82 additions and 25 deletions

View File

@ -1,3 +1,6 @@
// 兼容 Chrome、FireFox、Edge API
const browserAPI = typeof browser !== "undefined" ? browser : chrome;
// 配置对象:每个 hostname 对应一个跳转规则
const redirectMap = {
// 常规操作
@ -61,7 +64,6 @@ const redirectMap = {
// 特殊的url
"blog.51cto.com": {
method: function (tabId, url, rule) {
debugger
if (rule.path && !new URL(url).pathname.startsWith(rule.path)) {
return;
}
@ -77,8 +79,7 @@ const defaultConfig = {
param: ["target", "url", "q", "gourl", "u", "redirect", "toasturl", "link", "href"]
};
function handleRedirect(tabId, url) {
// debugger;
async function handleRedirect(tabId, url) {
const url_ = new URL(url);
const hostname = url_.hostname;
if (!redirectMap[hostname]) return;
@ -89,7 +90,7 @@ function handleRedirect(tabId, url) {
}
}
function redirectByUrl(tabId, url, rule) {
async function redirectByUrl(tabId, url, rule) {
const url_ = new URL(url);
const pathname = url_.pathname;
if (rule.path && !pathname.startsWith(rule.path)) {
@ -98,36 +99,55 @@ function redirectByUrl(tabId, url, rule) {
rule.param.forEach(param => {
const target = url_.searchParams.get(param);
if (target) {
chrome.tabs.update(tabId, { url: decodeURIComponent(target) });
browserAPI.tabs.update(tabId, { url: decodeURIComponent(target) });
}
});
}
function redirectByDocQuerySelector(tabId, url, rule) {
chrome.scripting.executeScript({
target: { tabId },
func: extractTargetFromHtml,
args: [rule.param]
});
async function redirectByDocQuerySelector(tabId, url, rule) {
if (browserAPI.scripting && browserAPI.scripting.executeScript) {
// Chrome / Edge (MV3)
browserAPI.scripting.executeScript({
target: { tabId },
func: extractTargetFromHtml,
args: [rule.param]
}).catch(err => console.error("Execute script error:", err));
} else {
// FireFox / Edge (MV2)
browserAPI.tabs.executeScript(tabId, {
code: `(${extractTargetFromHtml})("${rule.param}")`
}).catch(err => console.error("Execute script error:", err));
}
}
function extractTargetFromHtml(param) {
// debugger;
async function extractTargetFromHtml(param) {
const targetElement = document.querySelector(param);
if (targetElement) {
window.location.href = targetElement.textContent.trim();
}
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/onUpdated
// The status property will cycle through "loading" and "complete".
const lastTabUrls = new Map();
// 监听标签页更新事件
browserAPI.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
if (lastTabUrls.get(tabId) === tab.url) {
return;
}
lastTabUrls.set(tabId, tab.url);
handleRedirect(tabId, tab.url);
}
// chrome.runtime.reload();
});
chrome.runtime.onInstalled.addListener((details) => {
// 监听插件安装事件
browserAPI.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
chrome.tabs.create({ url: "./options.html" }); // 这里的 "welcome.html" 需要在插件的 `public` 目录下
if (browserAPI.tabs.create) {
browserAPI.tabs.create({ url: "./options.html" });
}
}
});

37
manifest-firefox.json Normal file
View File

@ -0,0 +1,37 @@
{
"manifest_version": 2,
"name": "__MSG_extName__",
"version": "2025.02.01.13.53",
"description": "__MSG_extDescription__",
"default_locale": "zh_CN",
"icons": {
"16": "icons/icon16.png",
"24": "icons/icon24.png",
"32": "icons/icon32.png",
"64": "icons/icon64.png",
"128": "icons/icon128.png"
},
"browser_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "icons/icon16.png",
"24": "icons/icon24.png",
"32": "icons/icon32.png",
"64": "icons/icon64.png",
"128": "icons/icon128.png"
}
},
"permissions": [
"tabs",
"activeTab",
"*://weixin110.qq.com/*",
"*://t.cn/*",
"*://jump.bdimg.com/*"
],
"options_page": "options.html",
"background": {
"scripts": ["background.js"],
"persistent": false
}
}

View File

@ -26,8 +26,8 @@
<p></p>
<h3 class="rainbow">善良的好人们行行好施舍点银子给我买包烟吧一块两块不嫌少三块四块会更好赠人香烟手有余香。——by爱抽烟的人</h3>
<p></p>
<image width="200" height="300" style="display: inline-block" src="images/WeChatPay.png"></image>
<image width="200" height="300" style="display: inline-block" src="images/AliPay.png"></image>
<img width="200" height="300" style="display: inline-block" src="images/WeChatPay.png">
<img width="200" height="300" style="display: inline-block" src="images/AliPay.png">
<script src="popup.js"></script>
</div>
</div>

View File

@ -4,7 +4,7 @@
EXT_DIR=$(cd "$(dirname "$0")" && pwd)
# 生成 YYYY.MM.DD.HH.MM 格式的时间戳
VERSION=$(date +%y.%m%d.%H%M)
VERSION=$(date +%y.%-m%d.%-H%M)
# 颜色输出
GREEN='\033[0;32m'
@ -18,7 +18,7 @@ echo "1) Chrome"
echo "2) Firefox"
echo "3) Edge"
echo "9) 删除打包文件"
read -p "输入选项 (1/2/3/4): " BROWSER_CHOICE
read -p "输入选项: " BROWSER_CHOICE
# 定义打包函数
pack_extension() {
@ -74,7 +74,7 @@ case $BROWSER_CHOICE in
delete_zip_file
;;
*)
echo -e "${RED}❌ 无效的选项!请重新运行脚本并输入 1 / 2 / 3 / 4${NC}"
echo -e "${RED}❌ 无效的选项!请重新运行脚本并输入${NC}"
exit 1
;;
esac

View File

@ -6,7 +6,7 @@
<title>My First Extension</title>
<style>
body {
width: 404px;
width: 405px;
/* height: 400px; */
font-family: Arial, sans-serif;
}
@ -34,8 +34,8 @@
<p></p>
<h3 class="rainbow">善良的好人们行行好施舍点银子给我买包烟吧一块两块不嫌少三块四块会更好赠人香烟手有余香。——by爱抽烟的人</h3>
<p></p>
<image width="200" height="300" style="display: inline-block" src="images/WeChatPay.png"></image>
<image width="200" height="300" style="display: inline-block" src="images/AliPay.png"></image>
<img width="200" height="300" style="display: inline-block" src="images/WeChatPay.png">
<img width="200" height="300" style="display: inline-block" src="images/AliPay.png">
<script src="popup.js"></script>
</body>