parrot-mocker
Advanced tools
Comparing version 1.3.0 to 1.4.1
@@ -7,3 +7,3 @@ chrome.tabs.onActivated.addListener(function(activeInfo) { | ||
chrome.extension.onMessage.addListener(function(message, sender, sendResponse){ | ||
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){ | ||
if (message.event === 'set-icon') { | ||
@@ -13,7 +13,11 @@ update_icon(message.active); | ||
}); | ||
update_icon(false); | ||
function update_icon(active) { | ||
chrome.browserAction.setIcon({ | ||
path: active ? 'img/icon-active-64.png' : 'img/icon-inactive-64.png' | ||
chrome.browserAction.setBadgeText({ | ||
text: active ? 'on' : 'off' | ||
}); | ||
chrome.browserAction.setBadgeBackgroundColor({ | ||
color: active ? [0, 100, 0, 200] : [128, 128, 128, 200] | ||
}); | ||
} |
@@ -7,8 +7,12 @@ var cst = require('../src/common/constants.js'); | ||
'mode-change': function(message) { | ||
if (!message.enabled) { | ||
removeCookie(cst.COOKIE_MOCK_ENABLED); | ||
} else { | ||
if (message.enabled) { | ||
// sync info from crx storage to page storage | ||
if (message.duration) localStorage.setItem(cst.LS_MOCK_DURATION, message.duration); | ||
if (message.skipRules) localStorage.setItem(cst.LS_MOCK_SKIP_RULES, message.skipRules); | ||
writeCookie(cst.COOKIE_MOCK_ENABLED, cst.COOKIE_MOCK_ENABLED_OK); | ||
writeCookie(cst.COOKIE_MOCK_SERVER, message.server); | ||
writeCookie(cst.COOKIE_MOCK_CLIENTID, message.clientid); | ||
} else { | ||
removeCookie(cst.COOKIE_MOCK_ENABLED); | ||
} | ||
@@ -53,2 +57,3 @@ send_icon_message(message.enabled); | ||
setTimeout(function() { | ||
// delay to wait the injected script updated the cookie according to url parameters | ||
send_icon_message(isActive()); | ||
@@ -61,3 +66,6 @@ }, 200); | ||
function writeCookie(key, value) { | ||
writeCookieHelper(key, value, 24 * 60 * 60); | ||
var duration = localStorage.getItem(cst.LS_MOCK_DURATION); | ||
duration = parseInt(duration) || 1; | ||
writeCookieHelper(key, value, 24 * 60 * 60 * duration); | ||
} | ||
@@ -72,3 +80,3 @@ function removeCookie(key) { | ||
function send_icon_message(active) { | ||
chrome.extension.sendMessage({ | ||
chrome.runtime.sendMessage({ | ||
event: 'set-icon', | ||
@@ -75,0 +83,0 @@ active: active |
{ | ||
"manifest_version": 2, | ||
"name": "ParrotMocker", | ||
"version": "1.3.0", | ||
"version": "1.4.1", | ||
"description": "Intercept h5/node.js requests and mock reponses", | ||
"icons": { | ||
"64": "img/icon-active-64.png" | ||
"128": "img/parrot-128.png" | ||
}, | ||
"browser_action": { | ||
"default_title": "ParrotMocker", | ||
"default_icon": "img/icon-active-64.png", | ||
"default_icon": "img/parrot-32.png", | ||
"default_popup": "popup.html" | ||
@@ -24,2 +24,3 @@ }, | ||
}, | ||
"options_page": "options.html", | ||
"permissions": [ | ||
@@ -26,0 +27,0 @@ "cookies", |
@@ -0,1 +1,10 @@ | ||
var cst = (function(exports) { | ||
// copy from src/common/constants.js | ||
exports.LS_MOCK_SERVER = '__mock_server'; | ||
exports.LS_MOCK_DURATION = '__mock_duration'; | ||
exports.LS_MOCK_SKIP_RULES = '__mock_skip_rules'; | ||
return exports; | ||
})({}); | ||
document.addEventListener('DOMContentLoaded', function() { | ||
@@ -6,2 +15,3 @@ chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | ||
var eleBtn = document.getElementsByClassName('btn')[0]; | ||
var eleSettings = document.getElementById('btn-settings'); | ||
@@ -14,7 +24,14 @@ var status = { | ||
clientid: '', | ||
server: '' | ||
server: '', | ||
duration: 1, | ||
skipRules: '' | ||
}; | ||
chrome.tabs.sendMessage(tabs[0].id, {event: 'query-status'}, function(res) { | ||
if (!res) return; | ||
if (!res) { | ||
eleMsg.innerHTML = 'No content.js loaded.'; | ||
updateEleBtn(eleBtn, status); | ||
return; | ||
} | ||
status.locked = res.locked; | ||
@@ -24,3 +41,3 @@ status.ishttps = res.ishttps; | ||
status.clientid = res.clientid || ''; | ||
status.server = decodeURIComponent(res.server || ''); | ||
status.server = decodeURIComponent(res.server || localStorage.getItem(cst.LS_MOCK_SERVER) || ''); | ||
@@ -30,2 +47,14 @@ updateStatus(); | ||
eleInput.addEventListener('focus', function() { | ||
this.select(); | ||
}); | ||
eleInput.addEventListener('keydown', function(e) { | ||
/* istanbul ignore else */ | ||
if (e.keyCode === 13) { // enter | ||
eleBtn.click(); | ||
} | ||
}); | ||
eleSettings.addEventListener('click', function() { | ||
chrome.runtime.openOptionsPage(); | ||
}); | ||
eleBtn.addEventListener('click', function() { | ||
@@ -60,2 +89,4 @@ if (status.locked) return; | ||
status.enabled = !status.enabled; | ||
status.duration = localStorage.getItem(cst.LS_MOCK_DURATION); | ||
status.skipRules = localStorage.getItem(cst.LS_MOCK_SKIP_RULES); | ||
@@ -106,3 +137,3 @@ chrome.tabs.sendMessage(tabs[0].id, status); | ||
eleBtn.innerHTML = 'Unable to Mock'; | ||
eleBtn.className = 'btn locked'; | ||
eleBtn.className = 'btn disabled'; | ||
} else if (status.enabled) { | ||
@@ -109,0 +140,0 @@ eleBtn.innerHTML = 'Click to Stop'; |
{ | ||
"name": "parrot-mocker", | ||
"version": "1.3.0", | ||
"version": "1.4.1", | ||
"description": "Intercept requests to the mock server", | ||
@@ -8,5 +8,7 @@ "browser": "./dist/parrot.js", | ||
"scripts": { | ||
"build": "sh build.sh", | ||
"prepublish": "npm run build", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "webpack", | ||
"zip": "zip -r dist/crx.zip crx", | ||
"prepublish": "npm run build && npm run zip", | ||
"test": "jest", | ||
"test:coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls" | ||
}, | ||
@@ -28,4 +30,8 @@ "keywords": [ | ||
"devDependencies": { | ||
"coveralls": "^3.0.0", | ||
"jest": "^22.4.2", | ||
"jsdom": "^11.7.0", | ||
"raw-loader": "^0.5.1", | ||
"webpack": "^1.13.1" | ||
"sinon-chrome": "^2.3.1", | ||
"webpack": "^3.1.0" | ||
}, | ||
@@ -32,0 +38,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
# parrot-mocker | ||
# parrot-mocker [![npm version](https://badge.fury.io/js/parrot-mocker.svg)](https://badge.fury.io/js/parrot-mocker) [![Build Status](https://travis-ci.org/chinesedfan/parrot-mocker.svg?branch=master)](https://travis-ci.org/chinesedfan/parrot-mocker) [![Coverage Status](https://coveralls.io/repos/github/chinesedfan/parrot-mocker/badge.svg?branch=master)](https://coveralls.io/github/chinesedfan/parrot-mocker?branch=master) [![License](https://img.shields.io/github/license/chinesedfan/parrot-mocker.svg)][license] | ||
@@ -44,2 +44,4 @@ This package supports to intercept XHR/JSONP/Fetch requests and forward to the specified [mock server](https://github.com/chinesedfan/parrot-mocker-web). | ||
- cookie `string` the cookie of the page request | ||
- shouldSkip `function` skip rewritting if returns true. By default, no host or including `local` will be filtered. Its arguments are: | ||
- host `string` the API host | ||
@@ -66,3 +68,3 @@ For example, let's make a simple middleware for Koa, | ||
MIT | ||
[MIT][license] | ||
@@ -74,1 +76,3 @@ ## Acknowledgement | ||
* [cortex-cookie-manager](https://github.com/cortexjs/cortex-cookie-manager), crx sample | ||
[license]: https://github.com/chinesedfan/parrot-mocker/blob/master/LICENSE |
@@ -6,2 +6,3 @@ var url = require('url'); | ||
var innerWrapUrl = require('./common/wrapurl.js'); | ||
var shouldSkip = require('./common/shouldskip.js'); | ||
@@ -40,3 +41,6 @@ var fetch = require('./wrapper/fetch-polyfill.js'); // force fetch to be implemented by xhr | ||
pageUrl: location.href, | ||
cookie: document.cookie // ATTENTION: the page cookies are different with the real API cookies | ||
cookie: document.cookie, // ATTENTION: the page cookies are different with the real API cookies | ||
shouldSkip: function(host) { | ||
return shouldSkip(host, localStorage.getItem(cst.LS_MOCK_SKIP_RULES)); | ||
} | ||
}, { | ||
@@ -49,3 +53,6 @@ isServer: false, | ||
function writeCookie(key, value) { | ||
cookies.setItem(key, value, 24 * 60 * 60, '/', location.hostname); | ||
var duration = localStorage.getItem(cst.LS_MOCK_DURATION); | ||
duration = parseInt(duration) || 1; | ||
cookies.setItem(key, value, 24 * 60 * 60 * duration, '/', location.hostname); | ||
} |
@@ -9,2 +9,6 @@ exports.GLOBAL_LOCK = '__mock_global_lock'; | ||
exports.LS_MOCK_SERVER = '__mock_server'; | ||
exports.LS_MOCK_DURATION = '__mock_duration'; | ||
exports.LS_MOCK_SKIP_RULES = '__mock_skip_rules'; | ||
exports.QUERY_MOCK_ENABLED = '__mock'; | ||
@@ -11,0 +15,0 @@ exports.QUERY_MOCK_SERVER = '__host'; |
@@ -14,8 +14,12 @@ var url = require('url'); | ||
module.exports = function(urlStr, options, innerOpts) { | ||
options = options || {}; | ||
var parsedUrl = url.parse(urlStr, true, true); | ||
// do not forward relative or local request, beacuse the server can not resolve | ||
if (!parsedUrl.host) return urlStr; | ||
if (parsedUrl.host.indexOf('local') >= 0) return urlStr; | ||
var shouldSkip = options.shouldSkip || function(host) { | ||
return !host || host.indexOf('local') >= 0; | ||
}; | ||
if (shouldSkip(parsedUrl.host)) return urlStr; | ||
var query = url.parse(options.pageUrl, true).query; | ||
var query = url.parse(options.pageUrl || '', true).query; | ||
var mock = query[cst.QUERY_MOCK_ENABLED] || cookies.getItem(options.cookie, cst.COOKIE_MOCK_ENABLED); | ||
@@ -22,0 +26,0 @@ var host = query[cst.QUERY_MOCK_SERVER] || cookies.getItem(options.cookie, cst.COOKIE_MOCK_SERVER); |
function init(wrapUrl) { | ||
var createElement = document.createElement; | ||
/* istanbul ignore else */ | ||
if (createElement) { | ||
@@ -7,17 +8,20 @@ document.createElement = function(tag) { | ||
if (tag.toLowerCase() == 'script') { | ||
var getValue = function(value) { | ||
return /(\?|\&)callback=/.test(value) ? wrapUrl(value, 'jsonp') : value; | ||
}; | ||
var setAttribute = node.setAttribute; | ||
node.setAttribute = function(label, value) { | ||
if (label.toLowerCase() == 'src' && /(\?|\&)callback=/.test(value)) { | ||
value = wrapUrl(value, 'jsonp'); | ||
if (label.toLowerCase() == 'src') { | ||
value = getValue(value); | ||
} | ||
setAttribute.call(node, label, value); | ||
}; | ||
Object.defineProperty(node, 'src', { | ||
set: function(value) { | ||
this.setAttribute('src', value); | ||
}, | ||
get: function() { | ||
this.getAttribute('src'); | ||
} | ||
}); | ||
var desc = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(node), 'src'); | ||
var setter = desc.set; | ||
desc.set = function(value) { | ||
setter.call(this, getValue(value)); | ||
}; | ||
Object.defineProperty(node, 'src', desc); | ||
} | ||
@@ -24,0 +28,0 @@ return node; |
function init(wrapUrl) { | ||
var xhr = window.XMLHttpRequest; | ||
/* istanbul ignore else */ | ||
if (xhr) { | ||
@@ -4,0 +5,0 @@ var open = xhr.prototype.open; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
226531
26
3176
0
76
6