I can't use variable with DOM in Action Extension.
The HTML page is like this.
My code
The code using "link" directly works. The code using 'var keyword = "link";' doesn't work. No matter the positions of the var line are.
Any idea?
The HTML page is like this.
Code Block html <!DOCTYPE html> <html lang="zh"> <head> </head> <body> <div class="wrap"> <p class="desc">如需浏览,请长按网址复制后使用浏览器访问</p> <p class="link">https://poster.parussoft.com/jumping.html?https://twitter.com/72473225/status/1300937074221043713</p> </div> </body> </html>
My code
Code Block javascript var Action = function() {}; // var keyword = "link"; Action.prototype = { run: function(arguments) { arguments.completionFunction({ "link" : document.getElementsByClassName("link")[0].innerHTML }) // var keyword = "link" // arguments.completionFunction({ keyword : document.getElementsByClassName(keyword)[0].innerHTML }) } ... }; var ExtensionPreprocessingJS = new Action
The code using "link" directly works. The code using 'var keyword = "link";' doesn't work. No matter the positions of the var line are.
Any idea?
A further investigation had shown that the issue was happened here.
There were two keywords used. arguments.completionFunction({ keyword : document.getElementsByClassName(keyword)[0].innerHTML }).
The issue was on the first keyword. It didn't convert to its value "link" but used its variable name "keyword" as the key. Maybe I should file a bug for this?
UPDATES
Just found this. https://stackoverflow.com/questions/10640159/key-for-javascript-dictionary-is-not-stored-as-value-but-as-variable-name
The code should changed to
Just using [] around keyword in key part.
I am not familiar with Javascript. But now I hate it. :(
Problem Solved!
Code Block swift arguments.completionFunction({ keyword : document.getElementsByClassName(keyword)[0].innerHTML })
There were two keywords used. arguments.completionFunction({ keyword : document.getElementsByClassName(keyword)[0].innerHTML }).
The issue was on the first keyword. It didn't convert to its value "link" but used its variable name "keyword" as the key. Maybe I should file a bug for this?
UPDATES
Just found this. https://stackoverflow.com/questions/10640159/key-for-javascript-dictionary-is-not-stored-as-value-but-as-variable-name
In fact, this was a well-known issue of how javascript interprets.
The code should changed to
Code Block swift arguments.completionFunction({ [keyword] : document.getElementsByClassName(keyword)[0].innerHTML })
Just using [] around keyword in key part.
I am not familiar with Javascript. But now I hate it. :(
Problem Solved!