Chrome浏览器扩展内容脚本直到刷新页面才加载 [英] Chrome extension Content Script not loaded until page is refreshed

查看:811
本文介绍了Chrome浏览器扩展内容脚本直到刷新页面才加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Chrome扩展内容脚本,我想在Trello板上运行。目前,它仅包含:

  console.log(Hello,world!); 

当您通过内部链接(如我的主板页面)打开Trello主板页面时,脚本不运行。它会在刷新页面后运行。



我的清单文件包含:

 manifest_version:2,

name:Temp Ext,
version:1.0,

content_scripts:[
{
matches:[*://trello.com/b/*],
js:[contentscript.js ]
}
]
}

任何人都可以帮助我为什么脚本在页面最初加载时不运行?



编辑:更正的问题。问题只出现在以下内部链接之后,而不是任何链接。 解决方案

问题在于Trello使用HTML5的pushState 进行页面转换,所以内容脚本并不总是在打开纸板后运行。



解决方案



清单变更:

<$
manifest_version:2,

name:Temp Ext,
version:1.1,

content_scripts:[{
matches:[*://trello.com/*],
js:[contentscript.js ]












$ b $ 权限:[
*://trello.com/*,tabs,webNavigation
]
}

添加后台脚本:

  chrome.webNavigation.onHistoryStateUpdated .addListener(function(details){
chr ome.tabs.executeScript(NULL,{文件: contentscript.js});
});


I have a Chrome extension content script that I want to run on Trello boards. For now, it contains only:

console.log("Hello, world!");

When you open the Trello board page through an internal link, like from the My Boards page, the content script does not run. It does run after you refresh the page though.

My manifest file contains:

{
  "manifest_version": 2,

  "name": "Temp Ext",
  "version": "1.0",

  "content_scripts": [
    {
      "matches": ["*://trello.com/b/*"],
      "js":["contentscript.js"]
    }
  ]
}

Can anyone help me figure out why the script doesn't run at the time the page is initially loaded?

EDIT: Corrected question. Issue only occurred after following internal links, not any links.

解决方案

The problem was that Trello uses HTML5's pushState for page transitions, so the content script wasn't always being run after a board was opened.

Solution

Changes to manifest:

{
  "manifest_version": 2,

  "name": "Temp Ext",
  "version": "1.1",

  "content_scripts": [{
    "matches": ["*://trello.com/*"],
    "js":["contentscript.js"]
  }],

  "background": {
    "scripts": ["background.js"]
  },

  "permissions": [
    "*://trello.com/*", "tabs", "webNavigation"
  ]
}

Add background script:

chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
    chrome.tabs.executeScript(null,{file:"contentscript.js"});
});

这篇关于Chrome浏览器扩展内容脚本直到刷新页面才加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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