This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-03-31 07:41:09

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

JS userscript not working!!!

It's an April fools prank that replaces some words with others, but it's not working!

This is the code:

Code:

function replace_words(words){
 for(word in words){
   document.innerHTML= document.innerHTML.replace(word, words[word]);
 }
}

replace_words({
"gangnam style" : "style",
" style" : "gangnam style"
"YouTube" : "<a href='http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8'>YouTube</a>", 
"open" : "oppan",
"Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!",
"poke" : "pok\u00E9mon",
"April" : "April Fools!",
"beautiful" : "pooey",
"9" = "hykgfsd",      // To prevent immediate replacement with 1
"8" = "9",
"7" = "8",
"6" = "7",
"5" = "6",
"4" = "5",
"3" = "4",
"2" = "3",
"1" = "2",
"0" = "1",
"hykgfsd" = "0",
"+" = "ksjdfhsdklthfdfkjug",  //Prevents immediate replacement with +
"-" = "+",
"ksjdfhsdklthfdfkjug" = "+",
"computer" : "toaster",
)};

I I need it working before tomorrow.

Can someone help please?


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#2 2013-03-31 08:03:42

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: JS userscript not working!!!

Line 3: It should be document.body.innerHTML in both places.

Also, starting with "9"="…", you use the = sign. You need to use the : (colon) like you did above.

The last line should be }), not )}.

Finally, to replace all of the words, not just the first occurrence, you need to use:

.replace(new RegExp(word, "g"), words[word])

So "cat cat".replace("cat", dog) only does "dog cat" right now.

Last edited by Hardmath123 (2013-03-31 08:07:20)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#3 2013-03-31 08:39:16

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: JS userscript not working!!!

OK, thanks, I did those thing but it's still not working. I looked in the console log and found this:

Code:

Uncaught SyntaxError: Unexpected string chrome-extension://nfannpdpemeggobgadhmiplhnhdbeoio/script.js:11

I'm assuming that means there's an error on line 11 which is :
"open" : "oppan",

I tried escaping the quote marks in the link on line 10 because i thought that might be the error, however I still get the same message  sad
Any suggestions please?
Here's the updated code:

Code:

function replace_words(words){
 for(word in words){
   document.body.innerHTML= document.body.innerHTML.replace(new RegExp(word, "g"), words[word]);
 }
}

replace_words({
"gangnam style" : "style",
" style" : "gangnam style"
"YouTube" : "<a href=\'http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8\'>YouTube</a>", 
"open" : "oppan",
"Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!",
"poke" : "pok\u00E9mon",
"April" : "April Fools!",
"beautiful" : "pooey",
"9" : "hykgfsd",      // To prevent imediate replacement with 1
"8" : "9",
"7" : "8",
"6" : "7",
"5" : "6",
"4" : "5",
"3" : "4",
"2" : "3",
"1" : "2",
"0" : "1",
"hykgfsd" : "0",
"+" : "ksjdfhsdklthfdfkjug",  //Prevents immediate replacement with +
"-" : "+",
"ksjdfhsdklthfdfkjug" : "+",
"computer" : "toaster",
});

My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#4 2013-03-31 09:56:54

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: JS userscript not working!!!

Looking at JSLint it seems you missed a comma on line 9.


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#5 2013-03-31 10:06:06

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: JS userscript not working!!!

Code:

function replace_words(words){
 for(word in words){
   document.body.innerHTML= document.body.innerHTML.replace(new RegExp(word, "g"), words[word]);
 }
}

replace_words({
"gangnam style" : "style",
" style" : "gangnam style",
"YouTube" : "<a href=\'http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8\'>YouTube</a>", 
"open" : "oppan",
"Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!",
"poke" : "pok\u00E9mon",
"April" : "April Fools!",
"beautiful" : "pooey",
"9" : "hykgfsd",      // To prevent imediate replacement with 1
"8" : "9",
"7" : "8",
"6" : "7",
"5" : "6",
"4" : "5",
"3" : "4",
"2" : "3",
"1" : "2",
"0" : "1",
"hykgfsd" : "0",
"+" : "ksjdfhsdklthfdfkjug",  //Prevents immediate replacement with +
"-" : "+",
"ksjdfhsdklthfdfkjug" : "+",
"computer" : "toaster",
});

http://i.imgur.com/BAEgGDL.png

Offline

 

#6 2013-03-31 10:20:37

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: JS userscript not working!!!

Yay! it works!!! Thanks, guys!

However, 4 little bugs:

- All numbers are turned into 0 instead of the intended purpose of making them all one higher (although this is still a good prank)

- + and - are not swapped

- Computer does not turn into toaster

- It is case sensitive.

Last edited by joefarebrother (2013-03-31 10:30:26)


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#7 2013-03-31 11:20:54

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: JS userscript not working!!!

For case-sensitivity, read up on RegExps.

For numbers turning to 0, think about your code for a minute. Say you're replacing 8. First, you replace it with 7. Then, you replace all 7s with 6s, so your 8 is now a 6. This goes on until you have 0 for all numbers.

+- is broken because you are converting + to gibberish and then the gibberish back to +. The "sdaipof":"+" line should be "sadlfjhk":"-".


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#8 2013-03-31 12:38:49

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: JS userscript not working!!!

Yay! It all works now!

Thanks!

Here's the whole code in case someone else wants to make a prank! Just change the words in the object!

Code:

function replace_words(words){
 for(word in words){
   document.body.innerHTML= document.body.innerHTML.replace(make_regexp(word), words[word]);
 }
}

function make_regexp(word){
  var str = "";
  for(i=0; i < word.length;                                         ++i){
    str += "[" + word.charAt(i).toLowerCase() + word.charAt(i).toUpperCase() + "]";
    
  }
  return new RegExp(str, "g");
}

replace_words({
"gangnam style" : "style",
" style" : " gangnam style",
"YouTube" : "<a href=\'http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8\'>YouTube</a>", 
"open" : "oppan",
"Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!",
"poke" : "pok\u00E9mon",
"April" : "April Fools!",
"beautiful" : "pooey",
"9" : "hykgfsd",      // To prevent imediate replacement with 1
"8" : "9",
"7" : "8",
"6" : "7",
"5" : "6",
"4" : "5",
"3" : "4",
"2" : "3",
"1" : "2",
"0" : "1",
"hykgfsd" : "0",
"+" : "ksjdfhsdklthfdfkjug",  //Prevents immediate replacement with +
"-" : "+",
"ksjdfhsdklthfdfkjug" : "-",
"computer" : "toaster",
});

Reporting to be closed...

Last edited by joefarebrother (2013-03-31 12:40:43)


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#9 2013-03-31 13:06:55

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: JS userscript not working!!!

Closed by request of the topic owner.

Note:  We've had a report that running this script can mess up your browser - so use at your own risk


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer