mirror of
https://gitee.com/bitdance-team/chrome-extension
synced 2025-01-10 21:58:14 +08:00
统一变量名;统一几个独立的onMessage Listener;popup.js中js代码优化
This commit is contained in:
parent
39571efd59
commit
253eeeaf9b
@ -1 +1,14 @@
|
||||
console.log("[BitDance extension] 学生助手插件已启用")
|
||||
|
||||
// 每次改变开关状态时刷新页面使功能及时生效
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
console.log(`进入 assets\js\content.js 中的onMessage Listener`)
|
||||
|
||||
if (request.action === 'refreshPage') {
|
||||
location.reload();
|
||||
sendResponse('Reload page because of ' + request.info);
|
||||
}
|
||||
|
||||
console.log(`离开 assets\js\content.js 中的onMessage Listener`)
|
||||
return true;
|
||||
})
|
||||
|
@ -1,8 +1,8 @@
|
||||
window.onload = function(){
|
||||
console.log("[BitDance extension] 学生助手插件 - 确认跳转页直接跳转模块加载成功");
|
||||
|
||||
chrome.storage.sync.get("linkOpen", ({ linkOpen })=>{
|
||||
if(linkOpen){
|
||||
chrome.storage.sync.get("State_DirectUrl", ({ State_DirectUrl })=>{
|
||||
if(State_DirectUrl){
|
||||
let locHost = location.host,locHref = location.href;
|
||||
|
||||
let methods = {
|
||||
|
@ -4,24 +4,10 @@
|
||||
|
||||
$(function() {
|
||||
console.log("[BitDance extension] 学生助手插件 - 点击特效模块加载成功");
|
||||
|
||||
// 每次改变开关状态时刷新页面使功能及时生效
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
// console.log(`进入 assets\js\mouse-effects\click.js 中的onMessage Listener`)
|
||||
|
||||
if (request.info === 'click') {
|
||||
location.reload();
|
||||
sendResponse('Reload page because of click');
|
||||
}
|
||||
|
||||
// console.log(`离开 assets\js\mouse-effects\click.js 中的onMessage Listener`)
|
||||
return true;
|
||||
})
|
||||
|
||||
// 控制功能是否开启
|
||||
chrome.storage.sync.get('clickState3', function(budget) {
|
||||
chrome.storage.sync.get('State_MouseEffect', function(budget) {
|
||||
// 得到按钮开关状态
|
||||
if (budget.clickState3 == false || budget.clickState3 == undefined) {
|
||||
if (budget.State_MouseEffect == false || budget.State_MouseEffect == undefined) {
|
||||
// console.log('click功能启动');
|
||||
var hearts = [];
|
||||
|
||||
|
@ -4,23 +4,10 @@
|
||||
|
||||
$(function() {
|
||||
console.log("[BitDance extension] 学生助手插件 - 鼠标样式模块加载成功");
|
||||
// 每次改变开关状态时刷新页面使功能及时生效
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
// console.log(`进入 assets\js\mouse-effects\mouse-style.js 中的onMessage Listener`)
|
||||
|
||||
if (request.info === 'mouse') {
|
||||
location.reload();
|
||||
sendResponse('Reload page because of mouse-style');
|
||||
}
|
||||
|
||||
// console.log(`离开 assets\js\mouse-effects\mouse-style.js 中的onMessage Listener`)
|
||||
return true;
|
||||
})
|
||||
|
||||
// 控制功能是否开启
|
||||
chrome.storage.sync.get('clickState2', function(budget) {
|
||||
chrome.storage.sync.get('State_MouseStyle', function(budget) {
|
||||
// 得到按钮开关状态
|
||||
if (budget.clickState2 == false || budget.clickState2 == undefined) {
|
||||
if (budget.State_MouseStyle == false || budget.State_MouseStyle == undefined) {
|
||||
let style = document.getElementsByTagName('style');
|
||||
// console.log(style[0]);
|
||||
let css = "* {cursor: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c%0A6QAABPxJREFUWEe9lltIalkYx9feak1hDBonIvVM0ZyKLmAM3YyIqB6KCgrK%0Abj710G1gKooJJrCYiB4q6qHefIgoKoaChpiieojQJIWiy0GygzaO0RVhSDTb%0Ae+1hOSlb3d56aL+Iur/1/db3/77/Whj4/6kGAOgAAPq37x/2gQEAyrRa7V9s%0ANhuTSqXk5eXlGUVRTR8FgwC+HB0dfc3JyWG7tq3RaAiZTAY/AgYBgNTU1Bed%0AThfBVHcE09DQQFxfX1+EUJkoAMAnAMDfoWroBMAwTAsh/ClYUACYIQzDfkeg%0ACQkJ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37BOzp%0A6bFNT0+73wkXwt8YIm8nMjIyMLPZ7JThzT7Z9N2jOedyuX+kp6dXHR8fvwvC%0A73UpMjJyxuFw/AwAcMoxODhIeJ9qSUlJdqPRiBILxWLxZTgQKSkpdr1eHxXw%0AvvZWzl2Kokq9S4vslc/n99EcLiyIycnJ1/7+/u9DAfi8u7v7rbS01GPEuFyu%0Aw2q1ehgSupKLxeLrYJVYX1931NXVyQEA46EAgLi4ONvd3Z3bE9CplpOTU+F9%0AsLxVKSAEPbm/05CpkT1kYLFYLxDCQHeGT5mZmddnZ2cejemdPBwAtwxoGlpb%0AW5MD+burEnQIpuThAIDY2Fj74+NjJI7jFnSTCTbvdIjOzk68u7t7GGnuHRdS%0AD7gWw3H8NwhhT4jJ6a8hKWxMcf8BWQiA/lSw+WUAAAAASUVORK5CYII=), auto}="
|
||||
|
@ -1,18 +1,101 @@
|
||||
$(function() {
|
||||
// 获取开关状态
|
||||
chrome.storage.sync.get('clickState2', function(budget) {
|
||||
// 获取页面节点
|
||||
let input = document.querySelector("#button-2");
|
||||
// 改变开关状态(保持与上次设置时一致)
|
||||
input.checked = budget.clickState2;
|
||||
/**
|
||||
* 鼠标样式
|
||||
*/
|
||||
// 页面加载时,更新界面开关状态
|
||||
chrome.storage.sync.get('State_MouseStyle', function(budget) {
|
||||
document.querySelector("#btnMouseStyle").checked = budget.State_MouseStyle;
|
||||
});
|
||||
// 获取开关状态
|
||||
chrome.storage.sync.get('clickState3', function(budget) {
|
||||
// 获取页面节点
|
||||
let input = document.querySelector("#button-3");
|
||||
// 改变开关状态(保持与上次设置时一致)
|
||||
input.checked = budget.clickState3;
|
||||
|
||||
// 点击开关时存储按钮状态并刷新页面
|
||||
$("#btnMouseStyle").click(function() {
|
||||
chrome.storage.sync.set({ 'State_MouseStyle': $("#btnMouseStyle")[0].checked });
|
||||
refreshPage('Mouse Style');
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* 鼠标特效
|
||||
*/
|
||||
// 页面加载时,更新界面开关状态
|
||||
chrome.storage.sync.get('State_MouseEffect', function(budget) {
|
||||
document.querySelector("#btnMouseEffect").checked = budget.State_MouseEffect;
|
||||
});
|
||||
|
||||
// 点击开关时存储按钮状态并刷新页面
|
||||
$("#btnMouseEffect").click(function() {
|
||||
chrome.storage.sync.set({ 'State_MouseEffect': $("#btnMouseEffect")[0].checked });
|
||||
refreshPage('Mouse Effect');
|
||||
})
|
||||
|
||||
|
||||
/**
|
||||
* Direct Url
|
||||
*/
|
||||
const btnDirectUrl = document.querySelector("#btnDirectUrl");
|
||||
// 页面加载时,更新界面开关状态
|
||||
chrome.storage.sync.get("State_DirectUrl", ({ State_DirectUrl }) => {
|
||||
btnDirectUrl.checked = !State_DirectUrl;
|
||||
});
|
||||
|
||||
// 点击开关时存储按钮状态并刷新页面
|
||||
btnDirectUrl.addEventListener("change", () => {
|
||||
chrome.storage.sync.set({ State_DirectUrl: !btnDirectUrl.checked });
|
||||
refreshPage('Direct Url');
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Google广告拦截
|
||||
*/
|
||||
const btnGoogleAds = document.querySelector("#btnAdsBlock");
|
||||
// 页面加载时,更新界面开关状态
|
||||
chrome.storage.sync.get('State_AdsBlock', function(budget) {
|
||||
btnGoogleAds.checked = !budget.State_AdsBlock;
|
||||
chrome.extension.getBackgroundPage().updateAdsBlockStatus(!btnGoogleAds.checked);
|
||||
});
|
||||
|
||||
// 点击开关时存储按钮状态并刷新页面
|
||||
$("#btnAdsBlock").click(function() {
|
||||
chrome.storage.sync.set({ 'State_AdsBlock': !btnGoogleAds.checked });
|
||||
chrome.extension.getBackgroundPage().updateAdsBlockStatus(!btnGoogleAds.checked);
|
||||
refreshPage('Ads Block');
|
||||
})
|
||||
|
||||
|
||||
/**
|
||||
* 截图
|
||||
*/
|
||||
document.getElementById("btnScreenshot").addEventListener("click", () => {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
if(isChromeSettingPage(tabs[0].url)) {
|
||||
alert("抱歉,由于浏览器限制,“chrome://”开头的网页不支持截图");
|
||||
} else {
|
||||
chrome.extension.getBackgroundPage().takeScreenshot(tabs[0]);
|
||||
window.close();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*/
|
||||
document.getElementById("transform").onclick = function () {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
if(isChromeSettingPage(tabs[0].url)) {
|
||||
alert("抱歉,由于浏览器限制,“chrome://”开头的网页不支持翻译");
|
||||
} else {
|
||||
chrome.extension.getBackgroundPage().showTranslationWindow()
|
||||
window.close();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 番茄钟
|
||||
*/
|
||||
// 获取番茄钟页面状态
|
||||
chrome.storage.sync.get('clockState', function(budget) {
|
||||
console.log(budget.clockState);
|
||||
@ -26,73 +109,6 @@ $(function() {
|
||||
}
|
||||
});
|
||||
|
||||
// 点击开关时改变按钮状态
|
||||
$("#button-2").click(function() {
|
||||
// 获取开关
|
||||
let checked = $("#button-2");
|
||||
// 持久化存储开关状态
|
||||
chrome.storage.sync.set({ 'clickState2': checked[0].checked });
|
||||
|
||||
// 自动刷新页面
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, (tabs) => {
|
||||
console.log(tabs);
|
||||
let message = {
|
||||
info: 'mouse'
|
||||
}
|
||||
chrome.tabs.sendMessage(tabs[0].id, message, res => {
|
||||
console.log(res);
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
// 点击开关时改变按钮状态
|
||||
$("#button-3").click(function() {
|
||||
// 获取开关
|
||||
let checked = $("#button-3");
|
||||
// 持久化存储开关状态
|
||||
chrome.storage.sync.set({ 'clickState3': checked[0].checked });
|
||||
|
||||
// 自动刷新页面
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, (tabs) => {
|
||||
console.log(tabs);
|
||||
let message = {
|
||||
info: 'click'
|
||||
}
|
||||
chrome.tabs.sendMessage(tabs[0].id, message, res => {
|
||||
console.log(res);
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Direct Url
|
||||
const btnDirectUrl = document.querySelector("#btnDirectUrl");
|
||||
chrome.storage.sync.get("linkOpen", ({ linkOpen }) => {
|
||||
btnDirectUrl.checked = !linkOpen;
|
||||
});
|
||||
|
||||
btnDirectUrl.addEventListener("change", () => {
|
||||
chrome.storage.sync.set({ linkOpen: !btnDirectUrl.checked });
|
||||
});
|
||||
|
||||
// 截图
|
||||
document.getElementById("btnScreenshot").addEventListener("click", () => {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
if(isChromeSettingPage(tabs[0].url)) {
|
||||
alert("抱歉,由于浏览器限制,“chrome://”开头的网页不支持截图");
|
||||
} else {
|
||||
chrome.extension.getBackgroundPage().takeScreenshot(tabs[0]);
|
||||
window.close();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// 番茄钟
|
||||
$('#clock').click(function() {
|
||||
$('#pomodoro').fadeIn(500);
|
||||
$('#article').fadeOut(0);
|
||||
@ -107,17 +123,7 @@ $(function() {
|
||||
chrome.storage.sync.set({ "clockState": "none" });
|
||||
})
|
||||
|
||||
//翻译
|
||||
document.getElementById("transform").onclick = function () {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
if(isChromeSettingPage(tabs[0].url)) {
|
||||
alert("抱歉,由于浏览器限制,“chrome://”开头的网页不支持翻译");
|
||||
} else {
|
||||
chrome.extension.getBackgroundPage().showTranslationWindow()
|
||||
window.close();
|
||||
}
|
||||
})
|
||||
}
|
||||
// ****************************************************************************************************************
|
||||
|
||||
/**
|
||||
* 判断是否是 chrome:// 开头的链接
|
||||
@ -127,4 +133,23 @@ $(function() {
|
||||
function isChromeSettingPage(url) {
|
||||
return /^chrome:\/\/.*$/.test(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 改变开关自动刷新页面
|
||||
*/
|
||||
function refreshPage(messageInfo) {
|
||||
chrome.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}, (tabs) => {
|
||||
console.log(tabs);
|
||||
let message = {
|
||||
info: messageInfo,
|
||||
action: "refreshPage"
|
||||
}
|
||||
chrome.tabs.sendMessage(tabs[0].id, message, res => {
|
||||
console.log(res);
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
@ -1,8 +1,15 @@
|
||||
var AdsBlockStatus = true;
|
||||
// refer: https://www.it1352.com/1996113.html
|
||||
chrome.webRequest.onBeforeRequest.addListener(
|
||||
function (details) {
|
||||
console.log("AdsBlock Status: ", AdsBlockStatus);
|
||||
|
||||
if (!AdsBlockStatus)
|
||||
return { cancel: false }; // 如果没有开启此功能,不处理
|
||||
|
||||
if (!details || !details.initiator || !details.url)
|
||||
return { cancel: false }; // 如果没有请求信息,则可能是chrome://开头的页面,不处理
|
||||
|
||||
let isBlock = false
|
||||
|| details.initiator === "https://googleads.g.doubleclick.net"
|
||||
|| details.initiator.indexOf("googleads.g.doubleclick.net") != -1
|
||||
@ -13,10 +20,22 @@ chrome.webRequest.onBeforeRequest.addListener(
|
||||
|| details.url.indexOf("adservice.google.com") != -1
|
||||
|| details.url.indexOf("googleads") != -1
|
||||
|| details.url.indexOf("adsbygoogle") != -1
|
||||
|
||||
if (isBlock) // 拦截后打印到控制台显示
|
||||
console.log(isBlock ? "block" : "allow", details)
|
||||
|
||||
return { cancel: isBlock };
|
||||
},
|
||||
{ urls: ["<all_urls>"] },
|
||||
["blocking"]
|
||||
);
|
||||
|
||||
// 由于上方拦截函数不可以是异步函数,所以设置变更后需要及时调用此函数
|
||||
var updateAdsBlockStatus = (status) => {
|
||||
AdsBlockStatus = !!status;
|
||||
}
|
||||
|
||||
// 插件刚开始加载时,先读取一次状态
|
||||
chrome.storage.sync.get('State_AdsBlock', function (State) {
|
||||
updateAdsBlockStatus(State.State_AdsBlock);
|
||||
});
|
||||
|
@ -64,9 +64,9 @@
|
||||
<ul>
|
||||
<li>
|
||||
<div class="setting-item">
|
||||
<span>广告屏蔽</span>
|
||||
<span>Google广告屏蔽</span>
|
||||
<div class="button">
|
||||
<input type="checkbox" id="button-1" class="checkbox" />
|
||||
<input type="checkbox" id="btnAdsBlock" class="checkbox" />
|
||||
<div class="knobs"></div>
|
||||
<div class="layer"></div>
|
||||
</div>
|
||||
@ -76,7 +76,7 @@
|
||||
<div class="setting-item">
|
||||
<span>鼠标样式</span>
|
||||
<div class="button">
|
||||
<input type="checkbox" id="button-2" class="checkbox" />
|
||||
<input type="checkbox" id="btnMouseStyle" class="checkbox" />
|
||||
<div class="knobs"></div>
|
||||
<div class="layer"></div>
|
||||
</div>
|
||||
@ -86,7 +86,7 @@
|
||||
<div class="setting-item">
|
||||
<span>点击特效</span>
|
||||
<div class="button">
|
||||
<input type="checkbox" id="button-3" class="checkbox" />
|
||||
<input type="checkbox" id="btnMouseEffect" class="checkbox" />
|
||||
<div class="knobs"></div>
|
||||
<div class="layer"></div>
|
||||
</div>
|
||||
@ -94,7 +94,7 @@
|
||||
</li>
|
||||
<li>
|
||||
<div class="setting-item">
|
||||
<span>访问页自动跳转</span>
|
||||
<span>确认访问页自动跳转</span>
|
||||
<div class="button">
|
||||
<input type="checkbox" id="btnDirectUrl" class="checkbox" />
|
||||
<div class="knobs"></div>
|
||||
|
Loading…
Reference in New Issue
Block a user