0

I've been looking around trying to find why chrome will properly hand off variables from the .user.js but Firefox will not. Below is my code that I've been working on and I can't seem to figure out the difference between the two browsers.

User 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*************************//

Search 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();

In Chrome, it's handing the variable from the .user.js to search.js fine. But in Firefox I'm getting "ReferenceError: searchTerm is not defined".

Any ideas as to what's different between Chrome and Firefox that would cause this kind of issue?

  • Could it be because Greasemonkey is [sandboxing](https://wiki.greasespot.net/Sandbox) content? I've never really tried to do what you're doing, so I've not come across this and it's purely a guess. It might be that Tampermonkey doesn't sandbox. It also seems like the [@unwrap](https://wiki.greasespot.net/Metadata_Block#.40unwrap) meta key has been removed, which _might_ have been useful to you. So...dunno - that's my best guess here. You might need to devise a different way to pass that value to your script. – VLAZ Aug 31 '16 at 22:51
  • Use `window.searchTerm = "pie";`. – Brock Adams Aug 31 '16 at 23:06

0 Answers0