rm chrome_tools

This commit is contained in:
suxiaoxin 2017-07-13 11:17:52 +08:00
parent 55690f1bbb
commit cb0d9cca67
10 changed files with 0 additions and 505 deletions

View File

@ -1,6 +0,0 @@
<!DOCTYPE html>
<html>
<body>
<script src="background.js"></script>
</body>
</html>

View File

@ -1,20 +0,0 @@
'use strict';
chrome.runtime.onMessage.addListener(function(request, _ , cb){
var data;
if(request.action === 'get'){
data = localStorage.getItem(request.name);
console.log(data)
if(typeof cb === 'function'){
cb(data)
}
}else if(request.action === 'set'){
console.log(request.name, request.value)
localStorage.setItem(request.name, request.value);
var newdata =data = localStorage.getItem(request.name);
console.log(newdata)
}
})

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

View File

@ -1,75 +0,0 @@
<html>
<body>
<h1>hello!!!</h1>
<input id="file" type="file" name="file" />
<button id="upload">upload</button>
<script src="yRequest.js"></script>
<script>
yRequest({
url: 'http://caibaojian.com/ajax-jsonp.html',
method: 'GET',
data: {
a:1
},
success: function(res, header){
console.log(header)
}
})
//test get
yRequest({
url: 'http://127.0.0.1:3000/api',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
a: 1,
b:2,
c: {
t:1
}
},
success: function(res){
console.log(arguments)
}
})
//test error get
yRequest({
url: 'http://127.0.0.1:3000/error_get',
method: 'GET',
error: function(err, header){
console.log(err)
}
})
//test upload
var file = document.getElementById('file');
document.getElementById('upload').addEventListener('click', function(){
yRequest({
url: 'http://127.0.0.1:3000/upload',
method: 'POST',
data: {
name: 'hello',
id: '19'
},
files: {
file: 'file'
},
success: function(res){
alert(res)
}
})
})
</script>
</body>
</html>

File diff suppressed because one or more lines are too long

View File

@ -1,23 +0,0 @@
{
"manifest_version": 2,
"name": "yapi-request",
"description": "这个chrome扩展赋予html页面跨域请求http(s)的能力",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [ "tabs", "storage", "webRequest", "webRequestBlocking", "*://*/*" ],
"background":
{
"scripts" : [
"background.js"
]
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": [
"response.js"
]
}]
}

View File

@ -1,34 +0,0 @@
<!doctype html>
<html>
<head>
<title>yRequest</title>
<meta charset="utf8"/>
<style>
body {font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif; font-size: 100%;}
.main{width:320px;}
*{margin:8px;}
.content{line-height: 25px;}
.add{line-height:40px;margin-bottom: 20px;}
.add input{width:200px;line-height:25px;}
.add button{width:70px;line-height:25px;}
.del{cursor:pointer}
</style>
</head>
<body>
<div class="main">
<h4>允许跨域请求的Host:</h4>
<div class="content" id="urls">
</div>
</div>
<div class="add" id="add">
<input />
<button class="submit">Add Host</button>
</div>
<script src="jquery-3.1.1.js"></script>
<script src="popup.js"></script>
</body>
</html>

View File

@ -1,56 +0,0 @@
function generateHtml(url) {
return `
<div class="item">
<span class="url">${url}</span>
<span class="del"></span>
</div>
`
}
var key = 'y_request_allow_urls';
var urls = chrome.runtime.sendMessage({action:'get', name: key}, function(urls){
var urlDom = $('#urls');
if (!urls || $.trim(urls) === '{}') {
urls = { 'http://yapi.corp.qunar.com': true ,
'127.0.0.1': true,
'0.0.0.0' : true
};
chrome.runtime.sendMessage({action:'set', name: key, value: JSON.stringify(urls)})
}
else urls = JSON.parse(urls);
for (var url in urls) {
urlDom.append(generateHtml(url));
}
$('#add .submit').bind('click', function () {
var val = $('#add input').val()
if (val) urls[val] = true;
chrome.runtime.sendMessage({
action:'set',
name: key,
value: JSON.stringify(urls)
})
urlDom.append(generateHtml(val))
})
urlDom.on('click', '.del', function (event) {
console.log('del')
var p = event.target.parentNode;
var url = $(p).find('.url').text();
delete urls[url]
chrome.runtime.sendMessage({
action:'set',
name: key,
value: JSON.stringify(urls)
})
p.parentNode.removeChild(p)
})
});

View File

@ -1,167 +0,0 @@
/*==============common begin=================*/
var container = 'y-request';
var INITSTATUS = 0;
var RUNSTATUS = 1;
var ENDSTATUS = 2;
var localStorageKey = 'y_request_allow_urls'
/*==============common end=================*/
var yRequestDom = document.getElementById(container);
function handleHeader(headers) {
if (!headers) return;
var newHeaders = {}, headers = headers.split(/[\r\n]/).forEach(function (header) {
var index = header.indexOf(":");
var name = header.substr(0, index);
var value = header.substr(index + 2);
if (name) {
newHeaders[name] = value;
}
})
return newHeaders;
}
function resFn(res, dom, data) {
if (!res) return;
var id = dom.getAttribute("_id");
var headers = handleHeader(this.getAllResponseHeaders());
data.res = {
id: id,
status: this.status,
statusText: this.statusText,
header: headers,
body: res
}
dom.innerText = JSON.stringify(data);
dom.setAttribute('status', ENDSTATUS);
}
function formUrlencode(data) {
return Object.keys(data).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
}).join('&')
}
function sendAjax(req, successFn, errorFn) {
var formDatas;
var xhr = new XMLHttpRequest();
xhr.timeout = req.timeout || 5000;
req.method = req.method || 'GET';
req.async = req.async === false ? false : true;
req.headers = req.headers || {};
if (req.method.toLowerCase() !== 'get') {
if (!req.headers['Content-Type'] || req.headers['Content-Type'] == 'application/x-www-form-urlencoded') {
req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
req.data = formUrlencode(req.data);
} else if (req.headers['Content-Type'] === 'multipart/form-data') {
formDatas = new FormData();
if (req.data) {
for (var name in req.data) {
formDatas.append(name, req.data[name]);
}
}
if (req.files) {
for (var name in req.files) {
var files = document.getElementById(req.files[name]).files;
if (files.length > 0) {
formDatas.append(name, files[0]);
}
}
}
req.data = formDatas;
} else if (typeof req.data === 'object' && req.data) {
req.data = JSON.stringify(req.data);
}
} else {
if (req.data) {
var getUrl = formUrlencode(req.data);
req.url = req.url + '?' + getUrl;
req.data = '';
}
}
xhr.open(req.method, req.url, req.async);
if (req.headers) {
for (var name in req.headers) {
xhr.setRequestHeader(name, req.headers[name]);
}
}
xhr.onload = function (e) {
if (this.status == 200) {
successFn.call(xhr, this.responseText);
} else {
errorFn.call(xhr, this.responseText)
}
};
xhr.ontimeout = function (e) {
errorFn.call(xhr, 'Error:Request timeout that the time is ' + xhr.timeout)
};
xhr.onerror = function (e) {
errorFn.call(xhr, xhr.statusText)
};
xhr.upload.onprogress = function (e) { };
xhr.send(req.data);
}
function yResponse() {
var reqsDom = yRequestDom.childNodes;
if (!reqsDom || reqsDom.length === 0) return;
reqsDom.forEach(function (dom) {
try {
var status = dom.getAttribute("status"), request;
if (+status === INITSTATUS) {
dom.setAttribute("status", RUNSTATUS);
var data = JSON.parse(dom.innerText);
var req = data.req;
sendAjax(req, function (res) {
resFn.bind(this)(res, dom, data);
}, function (err) {
resFn.bind(this)(err, dom, data);
})
}
} catch (error) {
console.error(error.message)
dom.parentNode.removeChild(dom)
}
})
}
function isAllowHost() {
chrome.runtime.sendMessage({ action: 'get', name: localStorageKey }, function (res) {
res = JSON.parse(res);
var flag = false;
for (var name in res) {
if (location.href.indexOf(name) > -1) {
flag = true;
}
}
if (flag && yRequestDom) {
console.log('yRequest running...')
setInterval(function () {
yResponse()
}, 100)
}
})
}
isAllowHost();

View File

@ -1,121 +0,0 @@
(function (win) {
/*==============common begin=================*/
var container = 'y-request';
var INITSTATUS = 0;
var RUNSTATUS = 1;
var ENDSTATUS = 2;
/*==============common end=================*/
function createNode(tagName, attributes, parentNode) {
options = attributes || {};
tagName = tagName || 'div';
var dom = document.createElement(tagName);
for (var attr in attributes) {
if (attr === 'id') dom.id = options[attr];
else dom.setAttribute(attr, options[attr]);
}
if (parentNode) parentNode.appendChild(dom);
return dom;
}
function getid() {
return container + '-' + id++;
}
var yRequestDom = createNode('div', { id: container, style: 'display:none' }, document.getElementsByTagName('body')[0]);
var yRequestMap = {};
var id = 0;
var interval;
function yRequest(req) {
if (!req) return;
if (typeof req === 'string') req = { url: req }
data = {
res: null,
req: req
}
data = JSON.stringify(data, null, 4);
var newId = getid();
var div = createNode('div', {
_id: newId,
status: INITSTATUS
}, yRequestDom);
div.innerText = data;
yRequestMap[newId] = {
id: newId,
status: INITSTATUS,
success: function (res, header, data) {
if (typeof req.success === 'function') {
req.success(res, header, data);
}
},
error: function (error, header, data) {
if (typeof req.error === 'function') {
req.error(error, header, data)
}
}
}
monitor();
}
function monitor() {
if (interval) return;
interval = setInterval(function () {
var queueDom = yRequestDom.childNodes;
if (!queueDom || queueDom.length === 0) {
interval = clearInterval(interval);
}
try {
for (var i = 0; i < queueDom.length; i++) {
try {
var dom = queueDom[i];
if (+dom.getAttribute('status') === ENDSTATUS) {
var text = dom.innerText;
if (text) {
var data = JSON.parse(dom.innerText);
var id = dom.getAttribute('_id');
var res = data.res;
if (res.status === 200) {
yRequestMap[id].success(res.body, res.header, data);
} else {
yRequestMap[id].error(res.statusText, res.header, data);
}
dom.parentNode.removeChild(dom);
} else {
dom.parentNode.removeChild(dom);
}
}
} catch (err) {
console.error(err.message);
dom.parentNode.removeChild(dom);
}
}
} catch (err) {
console.error(err.message);
interval = clearInterval(interval);
}
}, 50)
}
win.yRequest = yRequest;
if (typeof define == 'function' && define.amd) {
define('yRequest', [], function () {
return yRequest;
});
}
})(window)