如何在标签重新加载(铬扩展名)时运行此脚本?

编程入门 行业动态 更新时间:2024-10-27 10:22:55
本文介绍了如何在标签重新加载(铬扩展名)时运行此脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

{manifest_version:2, name:示例扩展名,description:示例Chrome扩展程序,version:1.0, content_scripts: [ {matches:[translate.google.hu/*], js:[run.js] } ], 权限: [activeTab,标签, browser_action: {default_title:示例,default_icon:图标。 png}

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

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

{manifest_version:2, name:示例扩展名,description:示例Chrome扩展程序,version:1.0,background:{scripts :[background.js] },权限:[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": ["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 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":[ "translate.google.hu/*", "tabs" ] }

background.js

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){ if (tab.url.indexOf("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.

更多推荐

如何在标签重新加载(铬扩展名)时运行此脚本?

本文发布于:2023-10-31 18:28:16,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时运   扩展名   脚本   加载   标签

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!