在这种情况下如何应用“chrome.webnavigation.oncommitted.addlistener”?

时间:2014-05-05 13:32:02

标签: google-chrome google-chrome-extension

我有一个Chrome扩展程序,用于更改网页图像中的颜色。激活扩展程序后(通过Chrome浏览器工具栏上的图标),标签中所有打开页面中的图像都会重新着色。更新选项卡的URL后,图像也会重新着色。但是,在显示重新着色的图像之前,显示了原始图像。 为了解决它,我隐藏了文档(通过" esconde.js")。然后拨打" recolor.js"它会对图像进行更改,最后会注入" mostra.js",使文档可见。 使用此代码,假设文档仅在图像重新着色后才可见,因为脚本" mostra.js"是一个回调脚本" recolor.js" 。我不知道为什么它没有按预期发生,因为在大多数页面中我仍然看到重新着色前的原始图像。 我读了几件事,我认为(或许)与" chrome.webnavigation.oncommitted.addlistener"而不是" chrome.tabs.onUpdated.addListener"它可以解决。 但是,我不知道如何替换" chrome.tabs.onUpdated.addListener" by" chrome.webnavigation.oncommitted.addlistener" (虽然它基本)。 请有人帮我完成这项任务吗?

chrome.tabs.onUpdated.addListener(function(tabid, info, tab) {
  if (flag){
    if (info.status != "complete") 
      chrome.tabs.executeScript(tabid, {file:"esconde.js", runAt: 'document_start' });
    if (info.status == "complete") {
      chrome.tabs.executeScript(tabid, {file:"recolor.js", runAt: 'document_start' }, function() {
        chrome.tabs.executeScript(tabid, {file:"mostra.js", runAt: 'document_start' });
      });
      chrome.browserAction.setIcon({path: "off.png", tabId:tab.id});
    }
  }
});

2 个答案:

答案 0 :(得分:1)

我猜chrome.webNavigation.onDOMContentLoaded会更适合这项任务:

chrome.webNavigation.onDOMContentLoaded.addListener(function(details) {
   chrome.tabs.executeScript(details.tabId, ...)
});

答案 1 :(得分:0)

具有箭头功能(大多数现代浏览器支持)和过滤器:

import cv2
import numpy as np

font = cv2.FONT_HERSHEY_COMPLEX

img = cv2.imread('img.png', cv2.IMREAD_COLOR)

# Remove image borders
img = img[20:-20, 20:-20, :]

imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# https://pysource.com/2018/09/25/simple-shape-detection-opencv-with-python-3/
# From the black and white image we find the contours, so the boundaries of all the shapes.
_, threshold = cv2.threshold(imgray, 127, 255, cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

c = contours[0]

peri = cv2.arcLength(c, closed=True)
approx = cv2.approxPolyDP(c, epsilon=0.01 * peri, closed=True)

# Delat threshold
t = 10

# n - Number of vertices
n = approx.shape[0]

for i in range(n):
    #      p1              p2
    #       *--------------*
    #       |
    #       |
    #       |
    #       *
    #      p0

    p0 = approx[(i+n-1) % n][0]    # Previous vertex
    p1 = approx[i][0]              # Current vertex
    p2 = approx[(i + 1) % n][0]    # Next vertex
    dx = p2[0] - p1[0]             # Delta pixels in horizontal direction
    dy = p2[1] - p1[1]             # Delta pixels in vertical direction

    # Fix x index of vertices p1 and p2 to be with same x coordinate ([<p1>, <p2>] form horizontal line).
    if abs(dx) < t:
        if ((dx < 0) and (p0[0] > p1[0])) or ((dx > 0) and (p0[0] < p1[0])):
            p2[0] = p1[0]
        else:
            p1[0] = p2[0]

    # Fix y index of vertices p1 and p2 to be with same y coordinate ([<p1>, <p2>] form vertical line).
    if abs(dy) < t:
        if ((dy < 0) and (p0[1] > p1[1])) or ((dy > 0) and (p0[1] < p1[1])):
            p2[1] = p1[1]
        else:
            p1[1] = p2[1]

    approx[i][0] = p1
    approx[(i + 1) % n][0] = p2

cv2.drawContours(img, [approx], 0, (0, 255, 0), 1)

# Finally we display everything on the screen:
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()