将变量从.user.js传递到js脚本,在Tampermonkey中工作正常但在Greasemonkey

时间:2016-08-31 22:44:20

标签: javascript google-chrome firefox greasemonkey tampermonkey

我一直在寻找为什么chrome会正确地从.user.js中移除变量,但Firefox不会。下面是我一直在研究的代码,我似乎无法弄清楚两种浏览器之间的区别。

用户JS

// ==UserScript==
// @name        Highlighter Public Test
// @namespace   https://ashencloud.net
// @grant       none
// @include     https://*.google.com*
// ==/UserScript==

//**********************USER SETTINGS***************************//
searchTerm = "pie";

//**********************START DO NOT EDIT***********************//
var script = document.createElement("script");
script.src = "https://ashencloud.net/grm/highlighter_public/search.js";
document.getElementsByTagName("head")[0].appendChild(script);
//**********************END DO NOT EDIT*************************//

搜索JS

// Create global variables for later
var timesRan = 1;
// Create function for general highlighting
// highlightWordGeneral will highlight the word even if it is part of the word or node
function highlightWordGeneral(word) {
    var xpath = "//text()[contains(., '" + word + "')]";
    var texts = document.evaluate(xpath, document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    for (n = 0; n < texts.snapshotLength; n++) {
        var textNode = texts.snapshotItem(n);
        var p = textNode.parentNode;
        var a = [];
        var frag = document.createDocumentFragment();
        textNode.nodeValue.split(word).forEach(function(text, i) {
            var node;
            if (i) {
                node = document.createElement('span');
                node.style.backgroundColor = "red";
                node.style.color = "white";
                node.appendChild(document.createTextNode(word));
                frag.appendChild(node);
            }
            if (text.length) {
                frag.appendChild(document.createTextNode(text));
            }
            return a;
        });
        p.replaceChild(frag, textNode);
    }
}
// Creates a function for searching all words at once later
function searchThings() {
  highlightWordGeneral(searchTerm);
}
//Creates a loop function that will run searchThings every 2000ms, stop after 30 times, and log it
function countRun () {                  //  create a loop function
   setTimeout(function () {             //  call a 3s setTimeout when the loop is called
      searchThings();                   //  run searchThings to find and highlight words
      timesRan++;                       //  increment the counter
      console.log("Searching...")       //  Log in the console to show evidence
      if (timesRan < 30) {              //  if the counter < 30, call the loop function
         countRun();                    //  ..  again which will trigger another
      }
   }, 2000)                             //  ..  setTimeout()
}
// Run function created above for countRun
countRun();

在Chrome中,它正在将变量从.user.js传递到search.js。但在Firefox中我得到“ReferenceError:searchTerm未定义”。

有关Chrome和Firefox之间有什么不同会产生此类问题的想法吗?

0 个答案:

没有答案