初始化

This commit is contained in:
xiananliu 2017-06-02 19:40:48 +08:00
parent 5bf1ff7cfd
commit 1bc804cf7d
10 changed files with 9674 additions and 0 deletions

23
demo/devtools.html Normal file
View 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
View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

32
demo/js/background.js Normal file
View 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
View 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

File diff suppressed because it is too large Load Diff

58
demo/manifest.json Normal file
View 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_actionpage_actionapp
"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
View 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
View 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
View 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>');
});
});