簡體   English   中英

chrome擴展的Javascript

[英]Javascript for chrome extension

我正在嘗試在頁面上創建一個按鈕

我的manifest.json具有內容腳本inject.js,而inject.js就像這樣

var botao_comunidades = document.querySelector('a[href="#Communities"]');

var botao_teste = document.createElement('p');
botao_teste.innerHTML = '<a href="#">Test</a>';
botao_teste.className = "themeLightTransparency NSC";
botao_comunidades.insertAdjacentElement('afterend',p);

的manifest.json

{
"name": "Teste",
"version": "0.0.1",
"manifest_version": 2,
"description": "Teste",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"permissions": [
"<all_urls>"
],
"content_scripts": [
{
  "matches": [
    "https://www.orkut.com.br/*"
  ],
  "js": [
    "src/inject/inject.js"
  ]
  ]
 }
]
}

它什么都不做:(

從清單中刪除關閉content_scripts.js的多余的[ ] (如果您想使此確切的代碼起作用,請在最后提及其他一些步驟)。 然后,當我加載腳本時,網站上的控制台顯示Uncaught ReferenceError: p is not defined在第6行Uncaught ReferenceError: p is not defined 。將最后一行更改為botao_comunidades.insertAdjacentElement('afterend',botao_teste); 一切正常。

使此確切代碼起作用的其他步驟:

  • 從清單中刪除“ icons”和“ default_locale”
  • 將content_script.matches更改為“ http://www.orkut.com.br/ *”
  • 更改inject.js的第一行以使用querySelector('#signUpViewport');

這是成品:

manifest.json的:

{
    "name": "Teste",
    "version": "0.0.1",
    "manifest_version": 2,
    "description": "Teste",
    "permissions": [
    "<all_urls>"
    ],
    "content_scripts": [
    {
        "matches": [
        "http://www.orkut.com.br/*"
        ],
        "js": [
        "src/inject/inject.js"
        ]
    }
    ]
}

SRC /注射/ inject.js:

var botao_comunidades = document.querySelector('#signUpViewport');

var botao_teste = document.createElement('p');
botao_teste.innerHTML = '<a href="#">Test</a>';
botao_teste.className = "themeLightTransparency NSC";
botao_comunidades.insertAdjacentElement('afterend',botao_teste);

屏幕截圖(“ Test”在左下方):

屏幕截圖


現在,輪到您對它不起作用的方式進行更多描述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM