5
我正在嘗試製作一個Chrome擴展程序,該擴展程序通過尋找關鍵字的網站進行解析,然後用按鈕替換這些關鍵字。但是,當我更改文本時,圖像路徑變得損壞。目前的結果將網頁文本中的匹配詞更改爲按鈕
// This is a content script (isolated environment)
// It will have partial access to the chrome API
// TODO
// Consider adding a "run_at": "document_end" in the manifest...
// don't want to run before full load
// Might also be able to do this via the chrome API
console.log("Scraper Running");
var keywords = ["sword", "gold", "yellow", "blue", "green", "china", "civil", "state"];
// This will match the keywords with the page textx
// Will also create the necessary buttons
(function() {
function runScraper() {
console.log($('body'));
for(var i = 0; i < keywords.length; i++){
$("body:not([href]):not(:image)").html($("body:not([href]):not(:image)").html()
.replace(new RegExp(keywords[i], "ig"),"<button> " + keywords[i] + " </button>"));
console.log("Ran it " + i);
}
}
function createResourceButton() {
// Programatically create a button here
// Really want to return the button
return null;
}
function createActionButton() {
}
runScraper();
})();
// TODO create the functions that the buttons will call
// These will pass data to the chrome extension (see message passing)
// Or we can consider a hack like this:
// "Building a Chrome Extension - Inject code in a page using a Content script"
// http://stackoverflow.com/questions/9515704
圖片:
您的選擇器捕獲'body'元素本身並更改其innerHTML,從而打破任何附加的事件偵聽器。另外,在修復選擇器時,您必須使用'.each()'來更改元素。 – wOxxOm
您是否確實想要將鏈接中顯示的單詞(即在''元素中)顯示到按鈕中,還是僅僅因爲鏈接斷開而請求?如果不打斷鏈接,您希望鏈接的顯示文本中的匹配詞語位於按鈕中嗎? – Makyen