简体   繁体   English

Chrome扩展程序-如何在每次加载页面时运行主脚本

[英]Chrome extension- How to have your main script run every time a page loads

So my main content script runs when i refresh the page however when I click on a new page of the webpage it doesnt run again; 因此,我的主要内容脚本在刷新页面时运行,但是当我单击网页的新页面时,它不会再次运行; only once when I refresh the page. 刷新页面时只有一次。 I want it to rerun every time the page changes or when the url changes. 我希望每次页面更改或URL更改时都重新运行。 The main function in the main content script is called "runExtension()" 主要内容脚本中的主要功能称为“ runExtension()”

This is my background.js 这是我的background.js

 chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) { if (changeInfo.status == 'complete') { alert("LOADED"); setTimeout(runExtension, 1000); } }) 

And this is my manifest.json 这是我的manifest.json

 { "manifest_version": 2, "name": "RateMyBull", "description": "View professor ratings from RateMyProfessor when signing up for classes on the USF schedule planner", "version": "1.0", "permissions": ["https://usf.collegescheduler.com/*"], "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches": ["https://usf.collegescheduler.com/*"], "js": ["RateMyBull.js", "jquery-3.3.1.min.js", "jquery-3.3.1.js"], "run_at": "document_idle", } ] } 
Here is the main script 这是主要脚本

 setTimeout(runExtension, 1000); function runExtension(){ var tableElementNode = document.querySelectorAll(".section-detail-grid.table-bordered-wrap>tbody>tr>td>div"); console.log(tableElementNode); for (var i = 0; i < tableElementNode.length; i++) { if(tableElementNode[i].children.length < 1 && tableElementNode[i].innerText != "Not Assigned") { tableElementNode[i].className = "professor"; } } var professorsArray = []; var professorElementNode = document.querySelectorAll(".professor"); for (var i = 0; i < professorElementNode.length; i++) { professorElementNode[i].id = i; var professorName = professorElementNode[i].innerText; professorName = professorName.trim(); professorName = professorName.replace(/,/g, ""); professorName = professorName.split([' ']); var firstName = professorName[1]; var lastName = professorName[0]; var professorObj = { first: firstName, last: lastName, ID: i }; professorsArray[i] = professorObj; console.log(professorsArray[i].first); } alert("this just ran"); } chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { if (msg === 'url-update') { alert("TEST"); } }); 

Content scripts like RateMyBull.js run in a totally separate environment from the background page (on, but somewhat isolated from , a webpage). 诸如RateMyBull.js类的Content scripts在与后台页面(在某个网页上, 但与该网页有些隔离 )的完全独立的环境中运行。

If you want your background page to cause code in a content script to run, use message passing . 如果您希望背景页面导致内容脚本中的代码运行,请使用消息传递

In your case, try changing background.js to: 就您而言,尝试将background.js更改为:

chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
  if (changeInfo.status == 'complete') {

    chrome.tabs.sendMessage(tab.id, {type: "runExtension"});

  }
})

And adding a listener to your RateMyBull.js content script: 并将侦听器添加到您的RateMyBull.js内容脚本中:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type === "runExtension")
      runExtension();
  });

Of course, there are lots of ways a webpage can "change". 当然,网页可以通过多种方式“更改”。 See the question and associated answers wOxxOm referenced in the comments for more insight on detecting various types of changes. 请参阅注释中引用的问题和相关答案,以获取有关检测各种类型更改的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次页面加载时如何运行脚本 - How to run script every time page loads 页面动态加载Chrome扩展程序内容时如何运行脚本 - How to run script when page loads content dynamically for Chrome Extension 使用Chrome扩展程序加载页面时如何运行脚本 - How to run a script when a page loads using Chrome extension 谷歌浏览器扩展名-无法将消息从内容脚本传递到同一扩展名中的弹出页面 - google chrome extension- unable to pass message from content script to popup page in same extension 为什么每次页面加载时我的脚本都不会运行? - Why won't my script run every time the page loads? 每次使用Google扩展插件加载页面时运行内容脚本吗? - Run content script every time a page loads with Google Extensions? javascript-页面加载后立即运行chrome扩展 - javascript - run chrome extension as soon as page loads 如何在每次加载特定页面时运行 JavaScript 脚本。 浏览器:Edge(我正在使用 Tampermonkey) - How do I make a JavaScript script run every time a specific page loads. Browser: Edge (I'm using tampermonkey) Chrome扩展程序:使其在每次页面加载时运行 - Chrome Extension: Make it run every page load 如何从Chrome扩展上下文在页面上运行脚本? - How to run script on page from chrome extension context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM