:
chrome.storage.local ( script)
, , , script, , . , .
script:
var updateTextTo = document.getElementById('comments').value;
chrome.storage.local.set({
updateTextTo: updateTextTo
}, function () {
chrome.tabs.executeScript({
file: "content_script3.js"
});
});
script:
chrome.storage.local.get('updateTextTo', function (items) {
assignTextToTextareas(items.updateTextTo);
chrome.storage.local.remove('updateTextTo');
});
function assignTextToTextareas(newText){
if (typeof newText === 'string') {
Array.from(document.querySelectorAll('textarea.comments')).forEach(el => {
el.value = newText;
});
}
}
. 1 2.
script,
script , script, script:
:
"'" + JSON.stringify().replace(/\\/g,'\\\\').replace(/'/g,"\\'") + "'" , JSON , code . .replace() A) , , B) ', . JSON.parse(), script. , , , script. , (.. ' / " , ). , - , , .
script:
var updateTextTo = document.getElementById('comments').value;
chrome.tabs.executeScript({
code: "var newText = JSON.parse('"
+ JSON.stringify(updateTextTo).replace(/\\/g,'\\\\').replace(/'/g,"\\'") + "';"
}, function () {
chrome.tabs.executeScript({
file: "content_script3.js"
});
});
script:
if (typeof newText === 'string') {
Array.from(document.querySelectorAll('textarea.comments')).forEach(el => {
el.value = newText;
});
}
. 1, 2 3.
(MDN) ( script)
, script , , , , script ( ). .
script:
var updateTextTo = document.getElementById('comments').value;
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {
file: "content_script3.js"
}, function(){
chrome.tabs.sendMessage(tabs[0].id,{
updateTextTo: updateTextTo
});
});
});
script:
# 3.2 . , , , , , , , .
chrome.runtime.onMessage.addListener(assignTextToTextareas);
function assignTextToTextareas(message){
newText = message.updateTextTo;
if (typeof newText === 'string') {
Array.from(document.querySelectorAll('textarea.comments')).forEach(el => {
el.value = newText;
});
}
chrome.runtime.onMessage.removeListener(assignTextToTextareas);
}
. 1 2.
1: Array.from() , , (Chrome >= 45, Firefox >= 32). Chrome Firefox Array.from() NodeList. Array asArray() . asArray(), , .
2: Chrome >= 51 Firefox >= 50, Chrome forEach() NodeLists v51. , . , , .
3: ( script ) , , .