【发布时间】:2016-01-28 13:45:36
【问题描述】:
我的扩展是这样工作的:
- 用户在扩展程序的弹出窗口中按下
Run按钮 - 弹出脚本从活动选项卡中收集一系列数据,例如文章链接、标题等
问题 #1:如何实现? - 循环使用收集的数据打开多个选项卡
- 一个内容脚本被注入到每个打开的标签中
- 每个打开的选项卡的内容脚本都应该从该数组的元素接收自己的数据
问题 #2:我该如何实现?
目前我正在使用chrome.storage.local 来存储收集到的整个数据数组,并希望将循环索引变量传递给每个内容脚本,以便内容脚本可以从chrome.storage.local 中读取正确的元素。
manifest.json:
{
"manifest_version": 2,
"name": "My Extension",
"description":"My article Extension",
"version": "1.0",
"background": {
"scripts": ["model/background.js"],
"persistent": true
},
"content_scripts": [{
"run_at": "document_start",
"matches": ["myurl"],
"js": ["model/contentscript.js", "model/jquery_2.1.4.min.js"]
}],
"browser_action": {
"default_popup": "template/popup.html",
"default_icon": "template/images/icon.png",
"default_title": "Start copy articles"
},
"icons": {
"16":"template/images/icon.png",
"48":"template/images/icon.png",
"128":"template/images/icon.png"
},
"permissions": [
"tabs",
"activeTab",
"storage",
"cookies"
]
}
popup.html
<!doctype html>
<head>
<title>Tapito Chrome extenstion</title>
<meta charset="utf-8">
</head>
<body>
<p>Variable</p><br>
<input type="text" value="" placehorlder="10" id="variable1" /><br>
<button id="runScript">Run</button>
<script src="popup.js" type="text/javascript"></script>
<div id="debugMode">
</div>
</body>
popup.js,不收集真实数据;使用了一个测试文章数组
function StartPastingArticles(){
var count = 3;
// test array
var articleArray = {
articles: [
{articleTitle: "title 1", articlePerex: "perex 1", articleAuthor: "author 1", articleDate: "date 1", articleUrl: "url 1"},
{articleTitle: "title 2", articlePerex: "perex 2", articleAuthor: "author 2", articleDate: "date 2", articleUrl: "url 2"},
{articleTitle: "title 3", articlePerex: "perex 3", articleAuthor: "author 3", articleDate: "date 3", articleUrl: "url 3"},
{articleTitle: "title 4", articlePerex: "perex 4", articleAuthor: "author 4", articleDate: "date 4", articleUrl: "url 4"},
{articleTitle: "title 5", articlePerex: "perex 5", articleAuthor: "author 5", articleDate: "date 5", articleUrl: "url 5"},
],
options: [
{time: '10', startDate: 'today'}
]
};
// actualy sending data via storage
chrome.storage.local.set({'storageObjectName': articleArray}, function () {});
var x = 1;
while (x < count){
chrome.tabs.create({'url': 'myurl'}, function(tab){
openWindow(tab.id);
});
x++;
}
}
// open new window and run script
function openWindow(tabId){
chrome.tabs.executeScript(tabId,{file:"contentscript.js"},function(){
});
}
// callback for popup htm
document.getElementById("runScript").addEventListener("click", function(e){
StartPastingArticles();
})
contentscript.js
document.addEventListener("DOMContentLoaded", function(event) {
pasteData();
});
var i;
/////////
function pasteData(){
chrome.storage.local.get('storageObjectName', function (data) {
chrome.extension.onMessage.addListener(function(message,sender,sendResponse){
var testData = data;
// article title
document.getElementById('data_name').value = testData["storageObjectName"].articles[0]["articleTitle"];
// article perex
document.getElementById('data_perex').value = testData["storageObjectName"].articles[0]["articlePerex"];
// article author
document.getElementById('data_source').value = testData["storageObjectName"].articles[0]["articleAuthor"];
// article url
document.getElementById('data_url').value = testData["storageObjectName"].articles[0]["articleUrl"];
// article date
document.getElementById('data_valid_from').value = testData["storageObjectName"].articles[0]["articleDate"];
});
}
【问题讨论】:
-
你好,我用我所有的代码编辑了任务。
-
您不需要
chrome.storage.local将所需的数据传递给内容脚本。我可以通过sendMessage告诉你一个更简单的方法,你可以吗? -
@wOxxOm 非常感谢。我还需要将 X 变量发送到新窗口...
标签: javascript google-chrome google-chrome-extension