我爲Firefox做了一個擴展,它隨機檢查網站上的所有單選按鈕&複選框。現在我將用於Chrome。Javascript DOM Chrome擴展簡單腳本
JS(inject.js
):
function randomFromTo(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
function autoFill() {
for (i = 0; i < document.forms.length ;i++) {
for (j = 0; j < document.forms[i].length ;j++) {
if (document.forms[i].elements[j].type == "radio") {
start = j;
lastName = document.forms[i].elements[j].name;
while (j < document.forms[i].length - 1 && lastName == document.forms[i].elements[j+1].name) {
j++;
}
rand = randomFromTo(start, j);
document.forms[i].elements[rand].checked = true;
}
if (document.forms[i].elements[j].type == "checkbox") {
start = j;
lastName = document.forms[i].elements[j].name;
while (j < document.forms[i].length - 1 && lastName == document.forms[i].elements[j+1].name) {
j++;
}
rand = randomFromTo(start, j);
document.forms[i].elements[rand].checked = true;
}
}
}
}
autoFill();
所以,我讀了很多關於inject.js
和內容腳本,所以我都嘗試。並做到這一點。
{
"name": "Auto Check Radio \u0026 Checkbox",
"version": "0.0.1",
"manifest_version": 2,
"description": "",
"homepage_url": "",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"background": {
"page": "src/bg/background.html",
"persistent": true
},
"browser_action": {
"default_icon": "icons/icon16.png",
"default_title": "Autocheck",
"default_popup": "src/browser_action/browser_action.html"
},
"permissions": [
"tabs",
"notifications",
"http://*/",
"http://*/*",
"https://*/*"
],
"content_scripts": [{
"matches": ["https://*/*", "http://*/*"],
"js": ["src/inject/inject.js"],
"run_at": "document_end"
}],
"web_accessible_resources": ["src/inject/inject.js"],
"js": ["src/inject/inject.js"]
}
]
}
但我不知道如何運行代碼。在Firefox上它更容易。
我不需要任何background.html
或browser_action.html
我只是想運行的腳本 - 通過點擊圖標 - 在當前標籤。
難道有人給我一個提示,我把我的腳本?
@克桑 - 非常感謝,現在它工作正常含。自動檢查 - 現在我可以清理它並構建一個包。 – frankdoo 2014-09-22 09:50:58
@frankdoo如果它適合你,請考慮將答案標記爲已接受。 – Xan 2014-09-22 15:20:47