【问题标题】:Chrome Extension Script/ Function not defined (Bootstrap Native?)Chrome 扩展脚本/功能未定义(Bootstrap Native?)
【发布时间】:2017-08-07 21:20:12
【问题描述】:

似乎我无法访问脚本中的 .popover 方法。除非我搞砸了,否则我应该可以从包含的 Bootstrap Native(Jquery-free Bootstrap)文件中访问该方法。 ?

脚本所做的只是添加链接,理想情况下是在该元素上添加一个弹出框。

这是我的代码:

清单:

{
  "name": "foo",
  "description": "Work in Progress",
  "manifest_version": 2,
  "version": "0.8",
  "icons": { 
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png" 
  },
  "permissions": [
    "activeTab",
    "http://*/",
    "https://*/"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_title": "Click me!"
  }
}

背景:(点击图标运行)

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "bootstrap-native.js" }, function() {
        chrome.tabs.executeScript(tab.id, {file: "content_script.js"});
});

});

content_script.js:

// Handle page's frame (to allow DOM access)
var page = top.frames["TargetContent"].document;

// Reference every professor listed and modify the registration page
Array.from(page.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => {
    if (el.textContent == "Staff") {
        return;
    }

    // For every professor found, search for RMP page
    searchProfessor(el)

});



/**
 * Search for professor on RMP, then pass along to pageCheck
 * 
 * @param {Reference to prof} professorEl 
 */
function searchProfessor(professorEl) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            pageCheck(this.response,professorEl);

        }
    }

    // Search RMP using CSUF + Prof Name 
    xhr.open('GET', 'https://www.ratemyprofessors.com/search.jsp?queryoption=HEADER&queryBy=teacherName&schoolName=california+state+university+fullerton&schoolID=&query=' + professorEl.textContent +'&_action_search=Search');
    xhr.responseType = 'document';
    xhr.send();
}



/**
 * Verify prof's page exists and modify registration page
 * 
 * @param {DOM Obj} page 
 * @param {Reference to prof} element 
 */
function pageCheck(page,element){

    var ProfURL = page.getElementsByClassName('listing PROFESSOR')[0].childNodes[1].href

    // If the element exists, we have a hit (and the prof's page!)
    if (ProfURL) {
        // Link to the prof's RMP page
        addAnchor(element, ProfURL);    

        // Access the prof's specific RMP page
        var xhr1 = new XMLHttpRequest();

        // Create box to display prof info on hover
        xhr1.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                addTooltip(this.response,element);
            }
        }

        xhr1.open('GET', ProfURL);
        xhr1.responseType = 'document';
        xhr1.send();

    }

}

function addTooltip(profPage,profElement) {

var name = profElement.textContent;
var grade = profPage.getElementsByClassName('grade')[0].textContent;    
var difficulty = profPage.getElementsByClassName('grade')[2].textContent;
var ratings = profPage.getElementsByClassName('table-toggle rating-count active')[0].textContent;
ratings = ratings.trim();
var content = "Grade: " + grade;

profElement.firstChild.setAttribute("data-toggle","popover");
profElement.firstChild.setAttribute("data-trigger","hover");
profElement.firstChild.setAttribute("title",name);
profElement.firstChild.setAttribute("data-content",content);
profElement.popover();

}

/**
 * Assign hyperlink to element 
 * 
 * @param {Element} wrapper 
 * @param {String} URL 
 */
function addAnchor (wrapper, URL) {

    var a = document.createElement('a');
    a.href = URL;
    a.textContent = wrapper.textContent;

    // Opens in new window/tab
    a.setAttribute('target', '_blank');
    wrapper.replaceChild(a, wrapper.firstChild);
}

引导原生链接:

https://github.com/thednp/bootstrap.native

http://thednp.github.io/bootstrap.native/

错误:

content_script.js:75 Uncaught TypeError: profElement.popover is not a function
    at addTooltip (content_script.js:75)
    at XMLHttpRequest.xhr1.onreadystatechange (content_script.js:61)

bootstrap-native 是您下载的 bootstrap native/dist/ 文件夹中的 68kb 文件。我认为这可能是问题所在,因为当我在这个文件中粘贴一个变量时,它可以在内容脚本中访问,但不能在方法中访问。

我对这一切都很陌生,所以也许我为 bootstrap native 提供的文件甚至不是正确的文件。或者我没有正确调用该方法,但这不应该给我这个错误。

【问题讨论】:

    标签: javascript google-chrome-extension bootstrap-native


    【解决方案1】:

    默认情况下,executeScript 注入only in the main page,而不是在 iframe 中。

    指定 allFrames: true 也可以在该 iframe 中注入 bootstrap-native:

    chrome.tabs.executeScript(tab.id, {allFrames: true, file: "bootstrap-native.js"}, ......
    

    【讨论】:

    • 为什么这适用于这里?
    • 因为top.frames["TargetContent"].document
    • @wOxxOm 嘿,忘了回复你,所以我重新制作了它。我试过了,它没有解决我的问题。我似乎无法使用任何引导本机方法。在我的脚本中。我将 allframes true 添加到第一个执行脚本并且没有骰子。
    • 在这种情况下,您应该在新问题中提到您的尝试,现在作为重复问题关闭。还要确保对 allFrames 参数使用正确的拼写。如果你这样做了,原因可能是 bootstrap-native 不能跨不同的框架工作。
    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2013-08-28
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多