簡體   English   中英

活動標簽上的html中的搜索元素(javascript)

[英]Search element in html on the active tab (javascript)

我正在嘗試創建自己的Google chrome擴展,以搜索html中的某些信息。重要提示:在活動標簽中搜索。

我的popup.js的示例

chrome.tabs.query( {
active: true,
lastFocusedWindow: true},

function(array_of_Tabs) {
var tab = array_of_Tabs[0];

chrome.tabs.executeScript(tab.id, {code: "chrome.runtime.sendMessage(document.getElementById('hour').innerHTML));"});

chrome.runtime.onMessage.addListener(function(request) {
document.getElementById('hour').innerHTML = request;});

我的manifest.json文件:

 {
 "name": "HTML opener",
 "description": "Open the current page in popup window",
 "version": "1.0",
 "permissions": [
 "activeTab"
  ],
  "browser_action": {
 "default_title": "HTML opener",
 "default_popup": "popup.html"
 },
 "manifest_version": 2
 }

我的popup.html看起來像:

<style>
  body {
    min-width: 300px;
    overflow-x: hidden;
  }
</style>
<script src="popup.js"></script>

我沒有任何結果。 有什么方法可以使用chrome.tabs在當前打開的選項卡上的html中查找文本嗎?

這是一個示例,該示例自動在當前頁面(標簽)上搜索元素id=hour

注意:我還添加了一些代碼來向用戶顯示操作圖標。

manifest.json

...
"page_action" :
{
    "default_icon" : "icon.png",
    "default_title" : "Icon title!"
},
"content_scripts" : [
{
  "matches" : [
    "http://*/*",
    "https://*/*"
  ],
  "js" : ["contentscript.js"],
  "run_at" : "document_idle",
  "all_frames" : false
}
],
...

contentscript.js

if (document.getElementById('hour')) {
    chrome.extension.sendRequest({}, function(response) {});
    // You can do whatever you want here ...
}

background.js

function onRequest(request, sender, sendResponse) {
    chrome.pageAction.show(sender.tab.id); // Chrome will show a pageAction icon
    sendResponse({});
};

chrome.extension.onRequest.addListener(onRequest);

暫無
暫無

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

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