我正在尝试创建一个 chrome 扩展来复制表单数据并粘贴它。我以某种方式取得了结果,但突然停止工作。我的 content_script1 和 content_script2 没有被加载。我对此有点陌生,并且对造成这种情况的原因感到困惑。我的代码是:
清单.json
{
"name": "Form Copy",
"version": "1.0",
"manifest_version": 2,
"description": "Form Copy and paste",
"permissions": [
"tabs", "<all_urls>",
"storage"
],
"browser_action": {
"default_icon": "img/icon.png",
"default_popup": "popup.html"
}
}
弹出窗口.html
<html>
<body>
<button type="button" id="btnCopy">Copy Source</button><br>
<button type="button" id="btnPaste">Paste to Target Page</button>
<script src="popup.js"></script>
</body>
</html>
Popup.js
function doContent(){
chrome.tabs.executeScript(null, {file: "content_script1.js"});
};
function doTarget(){
chrome.tabs.executeScript(null, {file: "content_script2.js"});
};
document.getElementById("btnCopy").onclick = doContent;
document.getElementById("btnPaste").onclick = doTarget;
content_script1.js
var input_data= document.getElementsByTagName("input");
var formArray= [];
for (var i = 0; i < input_data.length; i++) {
var id= input_data[i].getAttribute('id');
var name= input_data[i].getAttribute('name');
var value= input_data[i].getAttribute('value');
var type= input_data[i].getAttribute('type');
var storeArray = {
src_id: id,
src_type: type,
src_name: name,
src_value: value
};
formArray.push(storeArray);
}
console.log("Copying");
console.log("source page ran");
chrome.storage.local.set({
'newStorage': formArray
});
content_script2.js
console.log("Pasting");
var storedLegal = chrome.storage.local.get('newStorage', function (items) {
var storedArray= items.newStorage;
for (var i = 0; i < storedArray.length; i++) {
var id= storedArray[i].src_id;
var type= storedArray[i].src_type;
var name= storedArray[i].src_name;
var svalue= storedArray[i].src_value;
document.getElementById(id).value = svalue;
console.log(id+" "+type+" "+svalue+" "+name);
}
});
chrome.storage.local.clear(function() {
var error = chrome.runtime.lastError;
if (error) {
console.error(error);
}
});
我正在使用 test.html 来测试我的应用程序
测试.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<input type="text" id="fname" name="aaa" value="">
<input type="text" id="lname" name="bbb" value="">
<input type="text" id="phone" name="c" value="">
<input type="text" id="address" name="d" value="">
</form>
</body>
</html>