mirror of
https://gitee.com/bitdance-team/chrome-extension
synced 2025-11-04 20:03:10 +08:00
manifest.json升级到mv3(mv3的omnibox有Bug,见:https://bugs.chromium.org/p/chromium/issues/detail?id=1186804)
This commit is contained in:
@@ -1,16 +1,3 @@
|
|||||||
// 注册右键菜单
|
|
||||||
chrome.contextMenus.create({
|
|
||||||
id: 'bitdance-advanced-search',
|
|
||||||
title: '高级搜索',
|
|
||||||
parentId: 'bitdance',
|
|
||||||
onclick: function (info) {
|
|
||||||
alert('当前菜单信息:' + JSON.stringify(info))
|
|
||||||
alert("[BitDance extension] 学生助手插件 - 高级搜索 已点击菜单")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ss 的寓意
|
* ss 的寓意
|
||||||
*
|
*
|
||||||
@@ -43,186 +30,6 @@ chrome.contextMenus.create({
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ****************************************************************************************
|
|
||||||
*
|
|
||||||
* app.js/base.js
|
|
||||||
*
|
|
||||||
* ****************************************************************************************
|
|
||||||
*/
|
|
||||||
// 'use strict';
|
|
||||||
|
|
||||||
const app = {};
|
|
||||||
window.app = app;
|
|
||||||
|
|
||||||
/* runtime */
|
|
||||||
app.runtime = {
|
|
||||||
on(e, callback) {
|
|
||||||
if (e === 'start') {
|
|
||||||
chrome.runtime.onStartup.addListener(callback);
|
|
||||||
chrome.runtime.onInstalled.addListener(callback);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
get manifest() {
|
|
||||||
return chrome.runtime.getManifest();
|
|
||||||
},
|
|
||||||
connect(tabId, connectInfo) {
|
|
||||||
let port;
|
|
||||||
if (typeof tabId === 'object') {
|
|
||||||
port = chrome.runtime.connect(tabId);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
port = chrome.tabs.connect(tabId, connectInfo);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
on(e, callback) {
|
|
||||||
if (e === 'message') {
|
|
||||||
port.onMessage.addListener(callback);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
post(msg) {
|
|
||||||
port.postMessage(msg);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// /* storage */
|
|
||||||
// app.storage = {
|
|
||||||
// get(prefs, type = 'managed') {
|
|
||||||
// return new Promise(resolve => {
|
|
||||||
// if (type === 'managed') {
|
|
||||||
// chrome.storage.managed.get(prefs, ps => {
|
|
||||||
// chrome.storage.local.get(chrome.runtime.lastError ? prefs : ps || prefs, resolve);
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// chrome.storage[type].get(prefs, resolve);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// },
|
|
||||||
// set(prefs, type = 'managed') {
|
|
||||||
// return new Promise(resolve => {
|
|
||||||
// chrome.storage[type === 'remote' ? 'remote' : 'local'].set(prefs, resolve);
|
|
||||||
// });
|
|
||||||
// },
|
|
||||||
// on(e, callback) {
|
|
||||||
// if (e === 'changed') {
|
|
||||||
// chrome.storage.onChanged.addListener(callback);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /* button */
|
|
||||||
// app.button = {
|
|
||||||
// set({
|
|
||||||
// popup
|
|
||||||
// }, tabId) {
|
|
||||||
// if (popup !== undefined) {
|
|
||||||
// chrome.browserAction.setPopup({
|
|
||||||
// tabId,
|
|
||||||
// popup
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// on(e, callback) {
|
|
||||||
// if (e === 'clicked') {
|
|
||||||
// chrome.browserAction.onClicked.addListener(callback);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /* tab */
|
|
||||||
// app.tabs = {
|
|
||||||
// open({
|
|
||||||
// url
|
|
||||||
// }) {
|
|
||||||
// return new Promise(resolve => chrome.tabs.create({ url }, resolve));
|
|
||||||
// },
|
|
||||||
// current() {
|
|
||||||
// return new Promise(resolve => chrome.tabs.query({
|
|
||||||
// active: true,
|
|
||||||
// currentWindow: true
|
|
||||||
// }, (tabs = []) => resolve(tabs[0])));
|
|
||||||
// },
|
|
||||||
// inject: {
|
|
||||||
// js(tabId, details) {
|
|
||||||
// if (typeof tabId === 'object') {
|
|
||||||
// details = tabId;
|
|
||||||
// tabId = undefined;
|
|
||||||
// }
|
|
||||||
// return new Promise((resolve, reject) => {
|
|
||||||
// chrome.tabs.executeScript(tabId, Object.assign({
|
|
||||||
// runAt: 'document_start'
|
|
||||||
// }, details), results => {
|
|
||||||
// const lastError = chrome.runtime.lastError;
|
|
||||||
// if (lastError) {
|
|
||||||
// reject(lastError);
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// resolve(results);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
// },
|
|
||||||
// css(tabId, details) {
|
|
||||||
// if (typeof tabId === 'object') {
|
|
||||||
// details = tabId;
|
|
||||||
// tabId = undefined;
|
|
||||||
// }
|
|
||||||
// return new Promise((resolve, reject) => {
|
|
||||||
// chrome.tabs.insertCSS(tabId, Object.assign({
|
|
||||||
// runAt: 'document_start'
|
|
||||||
// }, details), results => {
|
|
||||||
// const lastError = chrome.runtime.lastError;
|
|
||||||
// if (lastError) {
|
|
||||||
// reject(lastError);
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// resolve(results);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /* window */
|
|
||||||
// app.windows = {
|
|
||||||
// open({url, left, top, width, height, type}) {
|
|
||||||
// width = width || 700;
|
|
||||||
// height = height || 500;
|
|
||||||
// if (left === undefined) {
|
|
||||||
// left = screen.availLeft + Math.round((screen.availWidth - width) / 2);
|
|
||||||
// }
|
|
||||||
// if (top === undefined) {
|
|
||||||
// top = screen.availTop + Math.round((screen.availHeight - height) / 2);
|
|
||||||
// }
|
|
||||||
// return new Promise(resolve => chrome.windows.create(
|
|
||||||
// {url, width, height, left, top, type: type || 'popup'},
|
|
||||||
// resolve
|
|
||||||
// ));
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
// /* menus */
|
|
||||||
// app.menus = {
|
|
||||||
// add(...items) {
|
|
||||||
// for (const item of items) {
|
|
||||||
// chrome.contextMenus.create(Object.assign({
|
|
||||||
// contexts: item.contexts || ['browser_action']
|
|
||||||
// }, item));
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// on(e, callback) {
|
|
||||||
// if (e === 'clicked') {
|
|
||||||
// chrome.contextMenus.onClicked.addListener(callback);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ****************************************************************************************
|
* ****************************************************************************************
|
||||||
*
|
*
|
||||||
@@ -302,19 +109,19 @@ var omniboxSearchModes = [
|
|||||||
switch (searchType) {
|
switch (searchType) {
|
||||||
default:
|
default:
|
||||||
case "[百度]":
|
case "[百度]":
|
||||||
navigate("https://www.baidu.com/s?wd="+ encodeURIComponent(searchText), true);
|
navigate("https://www.baidu.com/s?wd=" + encodeURIComponent(searchText), true);
|
||||||
break;
|
break;
|
||||||
case "[搜狗]":
|
case "[搜狗]":
|
||||||
navigate("https://www.sogou.com/web?query="+ encodeURIComponent(searchText), true);
|
navigate("https://www.sogou.com/web?query=" + encodeURIComponent(searchText), true);
|
||||||
break;
|
break;
|
||||||
case "[必应]":
|
case "[必应]":
|
||||||
navigate("https://cn.bing.com/search?q="+ encodeURIComponent(searchText), true);
|
navigate("https://cn.bing.com/search?q=" + encodeURIComponent(searchText), true);
|
||||||
break;
|
break;
|
||||||
case "[360]":
|
case "[360]":
|
||||||
navigate("https://www.so.com/s?q="+ encodeURIComponent(searchText), true);
|
navigate("https://www.so.com/s?q=" + encodeURIComponent(searchText), true);
|
||||||
break;
|
break;
|
||||||
case "[微博]":
|
case "[微博]":
|
||||||
navigate("https://s.weibo.com/weibo?q="+ encodeURIComponent(searchText), true);
|
navigate("https://s.weibo.com/weibo?q=" + encodeURIComponent(searchText), true);
|
||||||
break;
|
break;
|
||||||
case "[中国搜索]":
|
case "[中国搜索]":
|
||||||
navigate("http://www.chinaso.com/newssearch/all/allResults?q=" + encodeURIComponent(searchText), true);
|
navigate("http://www.chinaso.com/newssearch/all/allResults?q=" + encodeURIComponent(searchText), true);
|
||||||
@@ -657,9 +464,16 @@ chrome.omnibox.onInputEntered.addListener(function (text) {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
function encodeXML(str) {
|
function encodeXML(str) {
|
||||||
var holder = document.createElement('div');
|
// var holder = document.createElement('div');
|
||||||
holder.textContent = str;
|
// holder.textContent = str;
|
||||||
return holder.innerHTML;
|
// return holder.innerHTML;
|
||||||
|
|
||||||
|
// refer: stackoverflow.com/a/1091953/89484
|
||||||
|
return str.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/'/g, ''')
|
||||||
|
.replace(/"/g, '"')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -743,7 +557,7 @@ function updateDefaultSuggestion(text) {
|
|||||||
console.log(" text:", text);
|
console.log(" text:", text);
|
||||||
console.log(" 当前匹配搜索模式:", omniboxSearchModes[currentSearchModeIndex].showText);
|
console.log(" 当前匹配搜索模式:", omniboxSearchModes[currentSearchModeIndex].showText);
|
||||||
console.log(" isPlaintext:", isPlaintext);
|
console.log(" isPlaintext:", isPlaintext);
|
||||||
// console.log(description.join(''));
|
console.log(" omnibox显示", description.join(''));
|
||||||
console.log("[更新下拉框提示结束]");
|
console.log("[更新下拉框提示结束]");
|
||||||
|
|
||||||
chrome.omnibox.setDefaultSuggestion({
|
chrome.omnibox.setDefaultSuggestion({
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
chrome.contextMenus.create({
|
|
||||||
id: 'bitdance',
|
|
||||||
title: '学生助手'
|
|
||||||
})
|
|
||||||
|
|
||||||
// chrome.contextMenus.onClicked.addListener(function (info) {
|
|
||||||
// alert('当前菜单信息:'+ JSON.stringify(info))
|
|
||||||
// })
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//refer: https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/mv2-archive/api/notifications/background.js
|
|
||||||
function showNotification() {
|
|
||||||
var time = /(..)(:..)/.exec(new Date()); // The prettyprinted time.
|
|
||||||
var hour = time[1] % 12 || 12; // The prettyprinted hour.
|
|
||||||
var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // The period of the day.
|
|
||||||
new Notification(hour + time[2] + ' ' + period, {
|
|
||||||
icon: '48.png',
|
|
||||||
body: 'Time to make the toast.'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
chrome.contextMenus.create({
|
|
||||||
id: 'bitdance-advanced-search-notification',
|
|
||||||
title: 'Notification',
|
|
||||||
parentId: 'bitdance',
|
|
||||||
onclick: function (info) {
|
|
||||||
showNotification()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
56
packages/shell-chrome/assets/js/contextMenus.js
Normal file
56
packages/shell-chrome/assets/js/contextMenus.js
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
// 注册右键菜单
|
||||||
|
chrome.contextMenus.create({
|
||||||
|
id: 'bitdance',
|
||||||
|
title: '学生助手'
|
||||||
|
})
|
||||||
|
|
||||||
|
chrome.contextMenus.create({
|
||||||
|
parentId: 'bitdance',
|
||||||
|
id: 'bitdance-advanced-search',
|
||||||
|
title: '高级搜索(Todo)'
|
||||||
|
})
|
||||||
|
|
||||||
|
chrome.contextMenus.create({
|
||||||
|
parentId: 'bitdance',
|
||||||
|
id: 'bitdance-advanced-search-notification',
|
||||||
|
title: 'Notification',
|
||||||
|
})
|
||||||
|
|
||||||
|
chrome.contextMenus.onClicked.addListener(function (info, tab) {
|
||||||
|
console.log("info", info, "tab", tab)
|
||||||
|
// alert('当前菜单信息:' + JSON.stringify(info))
|
||||||
|
|
||||||
|
switch (info.menuItemId) {
|
||||||
|
case "bitdance-advanced-search-notification":
|
||||||
|
// 测试Notification
|
||||||
|
showNotification()
|
||||||
|
break;
|
||||||
|
case "bitdance-advanced-search":
|
||||||
|
// 高级搜索
|
||||||
|
console.log("[BitDance extension] 学生助手插件 - 高级搜索 已点击菜单")
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//refer: https://github.com/GoogleChrome/chrome-extensions-samples/blob/main/mv2-archive/api/notifications/background.js
|
||||||
|
function showNotification() {
|
||||||
|
// var time = /(..)(:..)/.exec(new Date()); // The prettyprinted time.
|
||||||
|
// var hour = time[1] % 12 || 12; // The prettyprinted hour.
|
||||||
|
// var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // The period of the day.
|
||||||
|
// new Notification(hour + time[2] + ' ' + period, {
|
||||||
|
// icon: '48.png',
|
||||||
|
// body: 'Time to make the toast.'
|
||||||
|
// });
|
||||||
|
|
||||||
|
// refer: https://developer.chrome.com/docs/extensions/mv3/richNotifications/#develop
|
||||||
|
chrome.notifications.create('', {
|
||||||
|
type: 'basic',
|
||||||
|
iconUrl: 'assets/image/logo.png',
|
||||||
|
title: '学生助手',
|
||||||
|
message: 'Time to make the toast.'
|
||||||
|
}, function (notificationId) {
|
||||||
|
console.log('notificationId: ' + notificationId)
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
"name": "学生助手",
|
"name": "学生助手",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"description": "BitDance Chrome Extension",
|
"description": "BitDance Chrome Extension",
|
||||||
"manifest_version": 2,
|
"manifest_version": 3,
|
||||||
"omnibox": {
|
"omnibox": {
|
||||||
"keyword": "ss"
|
"keyword": "ss"
|
||||||
},
|
},
|
||||||
@@ -12,11 +12,7 @@
|
|||||||
"128": "assets/image/logo.png"
|
"128": "assets/image/logo.png"
|
||||||
},
|
},
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": [
|
"service_worker": "service-worker.js"
|
||||||
"assets/js/lib/jquery.min.js",
|
|
||||||
"assets/js/background.js",
|
|
||||||
"assets/js/advanced-search/background.js"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
@@ -46,7 +42,7 @@
|
|||||||
"run_at": "document_start"
|
"run_at": "document_start"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"browser_action": {
|
"action": {
|
||||||
"default_icon": "assets/image/logo.png",
|
"default_icon": "assets/image/logo.png",
|
||||||
"default_popup": "popup.html",
|
"default_popup": "popup.html",
|
||||||
"default_title": "学生助手"
|
"default_title": "学生助手"
|
||||||
@@ -61,7 +57,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"web_accessible_resources": [
|
"web_accessible_resources": [
|
||||||
"/assets/image/*"
|
{
|
||||||
|
"resources": [
|
||||||
|
"/assets/image/*"
|
||||||
|
],
|
||||||
|
"matches": [
|
||||||
|
"*://*/*"
|
||||||
|
]
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"background",
|
"background",
|
||||||
@@ -69,10 +72,16 @@
|
|||||||
"storage",
|
"storage",
|
||||||
"unlimitedStorage",
|
"unlimitedStorage",
|
||||||
"webRequest",
|
"webRequest",
|
||||||
"webRequestBlocking",
|
|
||||||
"tabs",
|
"tabs",
|
||||||
"storage",
|
"storage",
|
||||||
"activeTab",
|
"activeTab",
|
||||||
"notifications"
|
"notifications"
|
||||||
]
|
],
|
||||||
|
"optional_permissions": [
|
||||||
|
"bookmarks"
|
||||||
|
],
|
||||||
|
"host_permissions": [
|
||||||
|
"*://*/*"
|
||||||
|
],
|
||||||
|
"content_security_policy": {}
|
||||||
}
|
}
|
||||||
|
|||||||
14
packages/shell-chrome/service-worker.js
Normal file
14
packages/shell-chrome/service-worker.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
try {
|
||||||
|
// 清除控制台
|
||||||
|
console.clear();
|
||||||
|
|
||||||
|
// refer: https://www.cuoshuo.com/blog/340811.html
|
||||||
|
importScripts(
|
||||||
|
// 右键菜单
|
||||||
|
"assets/js/contextMenus.js",
|
||||||
|
|
||||||
|
// 高级搜索
|
||||||
|
"assets/js/advanced-search/background.js"
|
||||||
|
);
|
||||||
|
|
||||||
|
} catch (e) { console.error(e) }
|
||||||
Reference in New Issue
Block a user