如何在标签重新加载(铬扩展名)时运行此脚本? [英] How can I run this script when the tab reloads (chrome extension)?

查看:102
本文介绍了如何在标签重新加载(铬扩展名)时运行此脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在标签重新加载到指定的URL时运行脚本。它几乎可以工作,但实际上id不是:)
这是我的清单文件:

  {
manifest_version:2,

name:示例扩展名,
description:示例Chrome扩展程序,
version:1.0,

content_scripts:
[
{
matches:[http://translate.google.hu/*],
js:[run.js]
}
],

权限:
[
activeTab,
标签


browser_action:
{
default_title:示例,
default_icon:图标。 png

}

这是run.js:
pre $ chrome.tabs.onUpdated.addListener(
函数(tabId,changeInfo,tab)
{
if(changeInfo.status ===complete)
{
chrome.tabs.executeScript(null,{file:program.js});
}
}
);

这些programs.js只是提醒一些文字(还)。当我将警报提示到run.js的第一行时,它会发出警报,但是当我将它放入if中时,它不会。我找不到问题。我输入了什么错误?

假设 http://translate.google.hu/* 页面是您希望在重新加载时注入代码的页面,您必须以稍微不同的方式进行操作。目前,您始终向这些页面注入代码(没有这样做的权限,不会少于这些),然后尝试使用 chrome.tabs api在那个内容脚本里面,你不能这样做。相反,我们会将侦听器放在后台页面中,并只在页面刷新时注入代码,就像您想要的那样。首先是清单

  {
manifest_version:2,
name:示例扩展名,
description:示例Chrome扩展程序,
version:1.0,
background:{
scripts :[background.js]
},
权限:[
http://translate.google.hu/*,tabs
]
}

background.js

  chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
if(tab.url.indexOf(http: //translate.google.hu/)> -1&&
changeInfo.url === undefined){
chrome.tabs.executeScript(tabId,{file:program.js });
}
});

这将侦听 onUpdated 事件,检查它是否是我们要注入的 url 之一,然后检查页面是否重新加载。最后一步是通过检查是否存在 changeInfo.url 来完成的。如果是这样,那么这意味着url已被更改,因此不会刷新。相反,如果它不存在,那么该页面只能被刷新。


So i'd like to run a script when the tab reloads in a specified URL. It almost works, but actually id doesn't :) This is my manifest file:

{
"manifest_version": 2,

"name": "Sample Extension",
"description": "Sample Chrome Extension",
"version": "1.0",

"content_scripts":
[
    {
      "matches": ["http://translate.google.hu/*"],
      "js": ["run.js"]
    }
],

"permissions":
[
    "activeTab",
    "tabs"
],

"browser_action":
{
    "default_title": "Sample",
    "default_icon": "icon.png"
}
}

and this is run.js:

chrome.tabs.onUpdated.addListener(
function ( tabId, changeInfo, tab )
{
    if ( changeInfo.status === "complete" )
    {
        chrome.tabs.executeScript( null, {file: "program.js"} );
    }
}
);

The programs.js just alerts some text (yet). When I put an alert to the first line of the run.js, it alerts, but when I put it in the if, it doesn't. I can't find the problem. Did I type something wrong?

解决方案

Assuming that http://translate.google.hu/* pages are the ones you wish to inject code into on reload, you would have to go about it in a slightly different way. Currently you are always injecting code into those pages (without the permission to do so, no less) and then trying to use the chrome.tabs api inside that content script, which you can't do. Instead, we will put the listener in a background page and inject the code only on a page refresh, like you want. First the manifest:

{
  "manifest_version": 2,
  "name": "Sample Extension",
  "description": "Sample Chrome Extension",
  "version": "1.0",
  "background": {
    "scripts": ["background.js"]
  },
  "permissions":[
    "http://translate.google.hu/*", "tabs"
  ]
}

background.js

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
  if (tab.url.indexOf("http://translate.google.hu/") > -1 && 
      changeInfo.url === undefined){
    chrome.tabs.executeScript(tabId, {file: "program.js"} );
  }
});

This will listen for the onUpdated event, checks if it is one of the url's that we want to inject into, and then it checks if the page was reloaded. That last step is accomplished by checking if changeInfo.url exists. If it does, then that means that the url was changed and thus not a refresh. Conversely, if it doesn't exist, then the page must have only been refreshed.

这篇关于如何在标签重新加载(铬扩展名)时运行此脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆