Я сделал расширение, которое выполняет глобальный поиск и замену на всех вкладках во всех входных и текстовых областях при нажатии enter
и при загрузке вкладки.
Репозиторий Github здесь .
всплывающее окно. html ↓
<!DOCTYPE html>
<header>
<title>Global Replace All</title>
</header>
<body>
<label>Find:
<input type="text" id="find" />
</label>
<label>Replace:
<input type="text" id="replace" />
</label>
<script src="popup.js"></script>
</body>
</html>
всплывающее окно. js ↓
'use strict';
const findBox = document.getElementById('find');
const replaceBox = document.getElementById('replace');
chrome.runtime.sendMessage({ message: 'get' }, response => {
findBox.value = response.find;
replaceBox.value = response.replace;
});
document.getElementById('find').addEventListener('input', () => {
const find = findBox.value;
chrome.runtime.sendMessage({ message: 'setFind', find });
});
document.getElementById('replace').addEventListener('input', () => {
const replace = replaceBox.value;
chrome.runtime.sendMessage({ message: 'setReplace', replace });
});
document.addEventListener('keypress', function (e) {
if (e.keyCode == 13) {
chrome.runtime.sendMessage({ message: 'replaceAll' });
window.close();
}
});
сценарий. js ↓
'use strict';
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message == 'replaceAll') {
replaceAll(request.find, request.replace);
sendResponse({ message: 'done ' });
}
});
window.onload = function () {
chrome.runtime.sendMessage({ message: 'tabLoaded' }, response => {
replaceAll(response.find, response.replace);
});
}
function replaceAll(find, replace) {
console.log(`Replacing all << ${find} >> with << ${replace} >>.`);
let myRegExp = new RegExp(find, 'g');
[].forEach.call(
document.querySelectorAll('input, textarea'),
function (e) { e.value = e.value.replace(myRegExp, replace); }
);
}
фон. js ↓
'use strict';
let find = '';
let replace = '';
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.message) {
case 'get':
sendResponse({ find, replace });
break;
case 'setFind':
find = request.find;
break;
case 'setReplace':
replace = request.replace;
break;
case 'tabLoaded':
sendResponse({ find, replace });
break;
case 'replaceAll':
return replaceAll();
break;
}
});
function replaceAll() {
new Promise(async resolve => {
let tabList = [];
chrome.windows.getAll({ populate: true }, windows => {
windows.forEach(window => {
window.tabs.forEach(tab => {
tabList.push(tab);
});
});
resolve(tabList);
});
}).then(tabList => {
tabList.forEach(tab => chrome.tabs.sendMessage(tab.id, { message: 'replaceAll', find, replace }));
});
return true; // this means its async
}
манифест. json ↓
{
"manifest_version": 2,
"name": "Global Replace All",
"permissions": [
"tabs", "activeTab"
],
"background": {
"persistent": false,
"scripts": [
"background.js"
]
},
"version": "0.1",
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
},
"browser_action": {
"default_icon": "icon16.png",
"default_title": "Global Replace All",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"script.js"
]
}
]
}