初始化
This commit is contained in:
parent
5bf1ff7cfd
commit
1bc804cf7d
23
demo/devtools.html
Normal file
23
demo/devtools.html
Normal file
@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>新标签页</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style>
|
||||
html,body{height: 100%;}
|
||||
body{font-family: 'Microsoft Yahei';margin:0;padding:0;}
|
||||
.center {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
font-size: 48px;
|
||||
color: #CCC;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="center"><p>这是一个自定义的新标签页</p></div>
|
||||
<script type="text/javascript" src="devtools.js"></script>
|
||||
</body>
|
||||
</html>
|
8
demo/devtools.js
Normal file
8
demo/devtools.js
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
chrome.devtools.panels.create("My Panel",
|
||||
"MyPanelIcon.png",
|
||||
"devtools.html",
|
||||
function(panel) {
|
||||
console.log(222);
|
||||
}
|
||||
);
|
BIN
demo/img/icon.png
Normal file
BIN
demo/img/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
32
demo/js/background.js
Normal file
32
demo/js/background.js
Normal file
@ -0,0 +1,32 @@
|
||||
// 右键菜单演示
|
||||
chrome.contextMenus.create({
|
||||
title: "测试右键菜单",
|
||||
onclick: function(){
|
||||
chrome.notifications.create(null, {
|
||||
type: 'basic',
|
||||
iconUrl: 'img/icon.png',
|
||||
title: '这是标题',
|
||||
message: '您刚才点击了自定义右键菜单!'
|
||||
});
|
||||
}
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
title: '使用度娘搜索:%s', // %s表示选中的文字
|
||||
contexts: ['selection'], // 只有当选中文字时才会出现此右键菜单
|
||||
onclick: function(params)
|
||||
{
|
||||
// 注意不能使用location.href,因为location是属于background的window对象
|
||||
chrome.tabs.create({url: 'https://www.baidu.com/s?ie=utf-8&wd=' + encodeURI(params.selectionText)});
|
||||
}
|
||||
});
|
||||
|
||||
// badge 演示
|
||||
// chrome.browserAction.setBadgeText({text: 'new'});
|
||||
// chrome.browserAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
|
||||
|
||||
chrome.tabs.onCreated.addListener(function(activeInfo)
|
||||
{
|
||||
//alert(activeInfo.tabId)
|
||||
console.log(activeInfo);
|
||||
chrome.pageAction.show(activeInfo.tabId);
|
||||
});
|
16
demo/js/content-script.js
Normal file
16
demo/js/content-script.js
Normal file
@ -0,0 +1,16 @@
|
||||
console.log('这是content script!');
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function()
|
||||
{
|
||||
// 给谷歌搜索结果的超链接增加 _target="blank"
|
||||
if(location.host == 'www.google.com.tw')
|
||||
{
|
||||
var objs = document.querySelectorAll('h3.r a');
|
||||
for(var i=0; i<objs.length; i++)
|
||||
{
|
||||
objs[i].setAttribute('_target', 'blank');
|
||||
}
|
||||
console.log('已处理谷歌超链接!');
|
||||
}
|
||||
});
|
||||
|
9472
demo/js/jquery-1.8.3.js
vendored
Normal file
9472
demo/js/jquery-1.8.3.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
58
demo/manifest.json
Normal file
58
demo/manifest.json
Normal file
@ -0,0 +1,58 @@
|
||||
{
|
||||
// 清单文件的版本,这个必须写,而且必须是2
|
||||
"manifest_version": 2,
|
||||
// 插件的名称
|
||||
"name": "demo",
|
||||
// 插件的版本
|
||||
"version": "1.0.0",
|
||||
// 插件描述
|
||||
"description": "简单的Chrome扩展demo",
|
||||
// 图标,一般偷懒全部用一个尺寸的也没问题
|
||||
"icons":
|
||||
{
|
||||
"16": "img/icon.png",
|
||||
"48": "img/icon.png",
|
||||
"128": "img/icon.png"
|
||||
},
|
||||
// 后台JS
|
||||
"background":
|
||||
{
|
||||
"scripts": ["js/background.js"]
|
||||
},
|
||||
// 浏览器右上角图标设置,browser_action、page_action、app必须三选一
|
||||
"browser_action":
|
||||
{
|
||||
"default_icon": "img/icon.png",
|
||||
"default_title": "这是一个示例Chrome插件",
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
// 当某些特定页面打开才显示的图标
|
||||
/*"page_action":
|
||||
{
|
||||
"default_icon": "img/icon.png",
|
||||
"default_title": "我是pageAction",
|
||||
"default_popup": "popup.html"
|
||||
},*/
|
||||
// 需要直接注入页面的JS
|
||||
"content_scripts":
|
||||
[
|
||||
{
|
||||
"matches": ["http://*/*", "https://*/*"],
|
||||
"js": ["js/content-script.js"]
|
||||
}
|
||||
],
|
||||
// 权限申请
|
||||
"permissions":
|
||||
[
|
||||
"contextMenus", // 右键菜单
|
||||
"tabs", // 标签
|
||||
"notifications"
|
||||
],
|
||||
// 插件主页,这个很重要,不要浪费了这个免费广告位
|
||||
"homepage_url": "https://www.baidu.com",
|
||||
"chrome_url_overrides":
|
||||
{
|
||||
"newtab": "newtab.html"
|
||||
},
|
||||
"devtools_page": "devtools.html"
|
||||
}
|
22
demo/newtab.html
Normal file
22
demo/newtab.html
Normal file
@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>新标签页</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style>
|
||||
html,body{height: 100%;}
|
||||
body{font-family: 'Microsoft Yahei';margin:0;padding:0;}
|
||||
.center {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
font-size: 48px;
|
||||
color: #CCC;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="center"><p>这是一个自定义的新标签页</p></div>
|
||||
</body>
|
||||
</html>
|
15
demo/popup.html
Normal file
15
demo/popup.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>popup</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style>
|
||||
body{font-family: 'Microsoft Yahei';}
|
||||
</style>
|
||||
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
|
||||
<script type="text/javascript" src="js/popup.js"></script>
|
||||
</head>
|
||||
<body style="width:300px;min-height:100px;">
|
||||
<h1>这是一个popup页面!</h1>
|
||||
</body>
|
||||
</html>
|
28
demo/popup.js
Normal file
28
demo/popup.js
Normal file
@ -0,0 +1,28 @@
|
||||
var bg = chrome.extension.getBackgroundPage();
|
||||
function add(name,url)
|
||||
{
|
||||
var path=bg.localStorage.path||'[]';
|
||||
path=JSON.parse(path);
|
||||
path.push([name,url]);
|
||||
bg.localStorage.path=JSON.stringify(path);
|
||||
return path;
|
||||
}
|
||||
function init()
|
||||
{
|
||||
var path=JSON.parse(bg.localStorage.path||'[]');
|
||||
var html='';
|
||||
for(var i=0;i<path.length;i++)
|
||||
html+='<tr><td>'+path[i][0]+'</td><td>'+path[i][1]+'</td></tr>';
|
||||
$('#table').append(html);
|
||||
}
|
||||
$(function()
|
||||
{
|
||||
init();
|
||||
$('#add').click(function()
|
||||
{
|
||||
var name=$('#name').val(),url=$('#url').val();
|
||||
add(name,url);
|
||||
$('#table').append('<tr><td>'+name+'</td><td>'+url+'</td></tr>');
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user